blob: 2e171b0209c5a427063a0c53817e49e4a88aa70e [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__
Kyungmin Park2d262c42008-11-19 16:26:54 +010011#include <linux/module.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020012#include <linux/slab.h>
13#include <linux/namei.h>
14#include <linux/fs.h>
Kyungmin Park2d262c42008-11-19 16:26:54 +010015#include <asm/div64.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020016#else
Kyungmin Park2d262c42008-11-19 16:26:54 +010017#include <ubi_uboot.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020018#endif
19#include <linux/err.h>
20
Kyungmin Park2d262c42008-11-19 16:26:54 +010021#include "ubi.h"
22
23/**
Heiko Schocherff94bc42014-06-24 10:10:04 +020024 * ubi_do_get_device_info - get information about UBI device.
25 * @ubi: UBI device description object
26 * @di: the information is stored here
27 *
28 * This function is the same as 'ubi_get_device_info()', but it assumes the UBI
29 * device is locked and cannot disappear.
30 */
31void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di)
32{
33 di->ubi_num = ubi->ubi_num;
34 di->leb_size = ubi->leb_size;
35 di->leb_start = ubi->leb_start;
36 di->min_io_size = ubi->min_io_size;
37 di->max_write_size = ubi->max_write_size;
38 di->ro_mode = ubi->ro_mode;
39#ifndef __UBOOT__
40 di->cdev = ubi->cdev.dev;
41#endif
42}
43EXPORT_SYMBOL_GPL(ubi_do_get_device_info);
44
45/**
Kyungmin Park2d262c42008-11-19 16:26:54 +010046 * ubi_get_device_info - get information about UBI device.
47 * @ubi_num: UBI device number
48 * @di: the information is stored here
49 *
50 * This function returns %0 in case of success, %-EINVAL if the UBI device
51 * number is invalid, and %-ENODEV if there is no such UBI device.
52 */
53int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
54{
55 struct ubi_device *ubi;
56
57 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
58 return -EINVAL;
Kyungmin Park2d262c42008-11-19 16:26:54 +010059 ubi = ubi_get_device(ubi_num);
60 if (!ubi)
61 return -ENODEV;
Heiko Schocherff94bc42014-06-24 10:10:04 +020062 ubi_do_get_device_info(ubi, di);
Kyungmin Park2d262c42008-11-19 16:26:54 +010063 ubi_put_device(ubi);
64 return 0;
65}
66EXPORT_SYMBOL_GPL(ubi_get_device_info);
67
68/**
Heiko Schocherff94bc42014-06-24 10:10:04 +020069 * ubi_do_get_volume_info - get information about UBI volume.
70 * @ubi: UBI device description object
71 * @vol: volume description object
Kyungmin Park2d262c42008-11-19 16:26:54 +010072 * @vi: the information is stored here
73 */
Heiko Schocherff94bc42014-06-24 10:10:04 +020074void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
75 struct ubi_volume_info *vi)
Kyungmin Park2d262c42008-11-19 16:26:54 +010076{
Kyungmin Park2d262c42008-11-19 16:26:54 +010077 vi->vol_id = vol->vol_id;
78 vi->ubi_num = ubi->ubi_num;
79 vi->size = vol->reserved_pebs;
80 vi->used_bytes = vol->used_bytes;
81 vi->vol_type = vol->vol_type;
82 vi->corrupted = vol->corrupted;
83 vi->upd_marker = vol->upd_marker;
84 vi->alignment = vol->alignment;
85 vi->usable_leb_size = vol->usable_leb_size;
86 vi->name_len = vol->name_len;
87 vi->name = vol->name;
88 vi->cdev = vol->cdev.dev;
89}
Heiko Schocherff94bc42014-06-24 10:10:04 +020090
91/**
92 * ubi_get_volume_info - get information about UBI volume.
93 * @desc: volume descriptor
94 * @vi: the information is stored here
95 */
96void ubi_get_volume_info(struct ubi_volume_desc *desc,
97 struct ubi_volume_info *vi)
98{
99 ubi_do_get_volume_info(desc->vol->ubi, desc->vol, vi);
100}
Kyungmin Park2d262c42008-11-19 16:26:54 +0100101EXPORT_SYMBOL_GPL(ubi_get_volume_info);
102
103/**
104 * ubi_open_volume - open UBI volume.
105 * @ubi_num: UBI device number
106 * @vol_id: volume ID
107 * @mode: open mode
108 *
109 * The @mode parameter specifies if the volume should be opened in read-only
110 * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
111 * nobody else will be able to open this volume. UBI allows to have many volume
112 * readers and one writer at a time.
113 *
114 * If a static volume is being opened for the first time since boot, it will be
115 * checked by this function, which means it will be fully read and the CRC
116 * checksum of each logical eraseblock will be checked.
117 *
118 * This function returns volume descriptor in case of success and a negative
119 * error code in case of failure.
120 */
121struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
122{
123 int err;
124 struct ubi_volume_desc *desc;
125 struct ubi_device *ubi;
126 struct ubi_volume *vol;
127
Heiko Schocherff94bc42014-06-24 10:10:04 +0200128 dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100129
130 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
131 return ERR_PTR(-EINVAL);
132
133 if (mode != UBI_READONLY && mode != UBI_READWRITE &&
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200134 mode != UBI_EXCLUSIVE && mode != UBI_METAONLY)
Kyungmin Park2d262c42008-11-19 16:26:54 +0100135 return ERR_PTR(-EINVAL);
136
137 /*
138 * First of all, we have to get the UBI device to prevent its removal.
139 */
140 ubi = ubi_get_device(ubi_num);
141 if (!ubi)
142 return ERR_PTR(-ENODEV);
143
144 if (vol_id < 0 || vol_id >= ubi->vtbl_slots) {
145 err = -EINVAL;
146 goto out_put_ubi;
147 }
148
149 desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
150 if (!desc) {
151 err = -ENOMEM;
152 goto out_put_ubi;
153 }
154
155 err = -ENODEV;
156 if (!try_module_get(THIS_MODULE))
157 goto out_free;
158
159 spin_lock(&ubi->volumes_lock);
160 vol = ubi->volumes[vol_id];
161 if (!vol)
162 goto out_unlock;
163
164 err = -EBUSY;
165 switch (mode) {
166 case UBI_READONLY:
167 if (vol->exclusive)
168 goto out_unlock;
169 vol->readers += 1;
170 break;
171
172 case UBI_READWRITE:
173 if (vol->exclusive || vol->writers > 0)
174 goto out_unlock;
175 vol->writers += 1;
176 break;
177
178 case UBI_EXCLUSIVE:
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200179 if (vol->exclusive || vol->writers || vol->readers ||
180 vol->metaonly)
Kyungmin Park2d262c42008-11-19 16:26:54 +0100181 goto out_unlock;
182 vol->exclusive = 1;
183 break;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200184
185 case UBI_METAONLY:
186 if (vol->metaonly || vol->exclusive)
187 goto out_unlock;
188 vol->metaonly = 1;
189 break;
Kyungmin Park2d262c42008-11-19 16:26:54 +0100190 }
191 get_device(&vol->dev);
192 vol->ref_count += 1;
193 spin_unlock(&ubi->volumes_lock);
194
195 desc->vol = vol;
196 desc->mode = mode;
197
198 mutex_lock(&ubi->ckvol_mutex);
199 if (!vol->checked) {
200 /* This is the first open - check the volume */
201 err = ubi_check_volume(ubi, vol_id);
202 if (err < 0) {
203 mutex_unlock(&ubi->ckvol_mutex);
204 ubi_close_volume(desc);
205 return ERR_PTR(err);
206 }
207 if (err == 1) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200208 ubi_warn(ubi, "volume %d on UBI device %d is corrupted",
Kyungmin Park2d262c42008-11-19 16:26:54 +0100209 vol_id, ubi->ubi_num);
210 vol->corrupted = 1;
211 }
212 vol->checked = 1;
213 }
214 mutex_unlock(&ubi->ckvol_mutex);
215
216 return desc;
217
218out_unlock:
219 spin_unlock(&ubi->volumes_lock);
220 module_put(THIS_MODULE);
221out_free:
222 kfree(desc);
223out_put_ubi:
224 ubi_put_device(ubi);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200225 ubi_err(ubi, "cannot open device %d, volume %d, error %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200226 ubi_num, vol_id, err);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100227 return ERR_PTR(err);
228}
229EXPORT_SYMBOL_GPL(ubi_open_volume);
230
231/**
232 * ubi_open_volume_nm - open UBI volume by name.
233 * @ubi_num: UBI device number
234 * @name: volume name
235 * @mode: open mode
236 *
237 * This function is similar to 'ubi_open_volume()', but opens a volume by name.
238 */
239struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
240 int mode)
241{
242 int i, vol_id = -1, len;
243 struct ubi_device *ubi;
244 struct ubi_volume_desc *ret;
245
Heiko Schocherff94bc42014-06-24 10:10:04 +0200246 dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100247
248 if (!name)
249 return ERR_PTR(-EINVAL);
250
251 len = strnlen(name, UBI_VOL_NAME_MAX + 1);
252 if (len > UBI_VOL_NAME_MAX)
253 return ERR_PTR(-EINVAL);
254
255 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
256 return ERR_PTR(-EINVAL);
257
258 ubi = ubi_get_device(ubi_num);
259 if (!ubi)
260 return ERR_PTR(-ENODEV);
261
262 spin_lock(&ubi->volumes_lock);
263 /* Walk all volumes of this UBI device */
264 for (i = 0; i < ubi->vtbl_slots; i++) {
265 struct ubi_volume *vol = ubi->volumes[i];
266
267 if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
268 vol_id = i;
269 break;
270 }
271 }
272 spin_unlock(&ubi->volumes_lock);
273
274 if (vol_id >= 0)
275 ret = ubi_open_volume(ubi_num, vol_id, mode);
276 else
277 ret = ERR_PTR(-ENODEV);
278
279 /*
280 * We should put the UBI device even in case of success, because
281 * 'ubi_open_volume()' took a reference as well.
282 */
283 ubi_put_device(ubi);
284 return ret;
285}
286EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
287
Heiko Schocherff94bc42014-06-24 10:10:04 +0200288#ifndef __UBOOT__
289/**
290 * ubi_open_volume_path - open UBI volume by its character device node path.
291 * @pathname: volume character device node path
292 * @mode: open mode
293 *
294 * This function is similar to 'ubi_open_volume()', but opens a volume the path
295 * to its character device node.
296 */
297struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode)
298{
299 int error, ubi_num, vol_id, mod;
300 struct inode *inode;
301 struct path path;
302
303 dbg_gen("open volume %s, mode %d", pathname, mode);
304
305 if (!pathname || !*pathname)
306 return ERR_PTR(-EINVAL);
307
308 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
309 if (error)
310 return ERR_PTR(error);
311
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200312 inode = d_backing_inode(path.dentry);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200313 mod = inode->i_mode;
314 ubi_num = ubi_major2num(imajor(inode));
315 vol_id = iminor(inode) - 1;
316 path_put(&path);
317
318 if (!S_ISCHR(mod))
319 return ERR_PTR(-EINVAL);
320 if (vol_id >= 0 && ubi_num >= 0)
321 return ubi_open_volume(ubi_num, vol_id, mode);
322 return ERR_PTR(-ENODEV);
323}
324EXPORT_SYMBOL_GPL(ubi_open_volume_path);
325#endif
326
Kyungmin Park2d262c42008-11-19 16:26:54 +0100327/**
328 * ubi_close_volume - close UBI volume.
329 * @desc: volume descriptor
330 */
331void ubi_close_volume(struct ubi_volume_desc *desc)
332{
333 struct ubi_volume *vol = desc->vol;
334 struct ubi_device *ubi = vol->ubi;
335
Heiko Schocherff94bc42014-06-24 10:10:04 +0200336 dbg_gen("close device %d, volume %d, mode %d",
337 ubi->ubi_num, vol->vol_id, desc->mode);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100338
339 spin_lock(&ubi->volumes_lock);
340 switch (desc->mode) {
341 case UBI_READONLY:
342 vol->readers -= 1;
343 break;
344 case UBI_READWRITE:
345 vol->writers -= 1;
346 break;
347 case UBI_EXCLUSIVE:
348 vol->exclusive = 0;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200349 break;
350 case UBI_METAONLY:
351 vol->metaonly = 0;
352 break;
Kyungmin Park2d262c42008-11-19 16:26:54 +0100353 }
354 vol->ref_count -= 1;
355 spin_unlock(&ubi->volumes_lock);
356
357 kfree(desc);
358 put_device(&vol->dev);
359 ubi_put_device(ubi);
360 module_put(THIS_MODULE);
361}
362EXPORT_SYMBOL_GPL(ubi_close_volume);
363
364/**
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200365 * leb_read_sanity_check - does sanity checks on read requests.
366 * @desc: volume descriptor
367 * @lnum: logical eraseblock number to read from
368 * @offset: offset within the logical eraseblock to read from
369 * @len: how many bytes to read
370 *
371 * This function is used by ubi_leb_read() and ubi_leb_read_sg()
372 * to perform sanity checks.
373 */
374static int leb_read_sanity_check(struct ubi_volume_desc *desc, int lnum,
375 int offset, int len)
376{
377 struct ubi_volume *vol = desc->vol;
378 struct ubi_device *ubi = vol->ubi;
379 int vol_id = vol->vol_id;
380
381 if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
382 lnum >= vol->used_ebs || offset < 0 || len < 0 ||
383 offset + len > vol->usable_leb_size)
384 return -EINVAL;
385
386 if (vol->vol_type == UBI_STATIC_VOLUME) {
387 if (vol->used_ebs == 0)
388 /* Empty static UBI volume */
389 return 0;
390 if (lnum == vol->used_ebs - 1 &&
391 offset + len > vol->last_eb_bytes)
392 return -EINVAL;
393 }
394
395 if (vol->upd_marker)
396 return -EBADF;
397
398 return 0;
399}
400
401/**
Kyungmin Park2d262c42008-11-19 16:26:54 +0100402 * ubi_leb_read - read data.
403 * @desc: volume descriptor
404 * @lnum: logical eraseblock number to read from
405 * @buf: buffer where to store the read data
406 * @offset: offset within the logical eraseblock to read from
407 * @len: how many bytes to read
408 * @check: whether UBI has to check the read data's CRC or not.
409 *
410 * This function reads data from offset @offset of logical eraseblock @lnum and
411 * stores the data at @buf. When reading from static volumes, @check specifies
412 * whether the data has to be checked or not. If yes, the whole logical
413 * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
414 * checksum is per-eraseblock). So checking may substantially slow down the
415 * read speed. The @check argument is ignored for dynamic volumes.
416 *
417 * In case of success, this function returns zero. In case of failure, this
418 * function returns a negative error code.
419 *
420 * %-EBADMSG error code is returned:
421 * o for both static and dynamic volumes if MTD driver has detected a data
422 * integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
423 * o for static volumes in case of data CRC mismatch.
424 *
425 * If the volume is damaged because of an interrupted update this function just
426 * returns immediately with %-EBADF error code.
427 */
428int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
429 int len, int check)
430{
431 struct ubi_volume *vol = desc->vol;
432 struct ubi_device *ubi = vol->ubi;
433 int err, vol_id = vol->vol_id;
434
Heiko Schocherff94bc42014-06-24 10:10:04 +0200435 dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100436
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200437 err = leb_read_sanity_check(desc, lnum, offset, len);
438 if (err < 0)
439 return err;
Kyungmin Park2d262c42008-11-19 16:26:54 +0100440
Kyungmin Park2d262c42008-11-19 16:26:54 +0100441 if (len == 0)
442 return 0;
443
444 err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000445 if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200446 ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100447 vol->corrupted = 1;
448 }
449
450 return err;
451}
452EXPORT_SYMBOL_GPL(ubi_leb_read);
453
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200454#ifndef __UBOOT__
455/**
456 * ubi_leb_read_sg - read data into a scatter gather list.
457 * @desc: volume descriptor
458 * @lnum: logical eraseblock number to read from
459 * @buf: buffer where to store the read data
460 * @offset: offset within the logical eraseblock to read from
461 * @len: how many bytes to read
462 * @check: whether UBI has to check the read data's CRC or not.
463 *
464 * This function works exactly like ubi_leb_read_sg(). But instead of
465 * storing the read data into a buffer it writes to an UBI scatter gather
466 * list.
467 */
468int ubi_leb_read_sg(struct ubi_volume_desc *desc, int lnum, struct ubi_sgl *sgl,
469 int offset, int len, int check)
470{
471 struct ubi_volume *vol = desc->vol;
472 struct ubi_device *ubi = vol->ubi;
473 int err, vol_id = vol->vol_id;
474
475 dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
476
477 err = leb_read_sanity_check(desc, lnum, offset, len);
478 if (err < 0)
479 return err;
480
481 if (len == 0)
482 return 0;
483
484 err = ubi_eba_read_leb_sg(ubi, vol, sgl, lnum, offset, len, check);
485 if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
486 ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
487 vol->corrupted = 1;
488 }
489
490 return err;
491}
492EXPORT_SYMBOL_GPL(ubi_leb_read_sg);
493#endif
494
Kyungmin Park2d262c42008-11-19 16:26:54 +0100495/**
496 * ubi_leb_write - write data.
497 * @desc: volume descriptor
498 * @lnum: logical eraseblock number to write to
499 * @buf: data to write
500 * @offset: offset within the logical eraseblock where to write
501 * @len: how many bytes to write
Kyungmin Park2d262c42008-11-19 16:26:54 +0100502 *
503 * This function writes @len bytes of data from @buf to offset @offset of
Heiko Schocherff94bc42014-06-24 10:10:04 +0200504 * logical eraseblock @lnum.
Kyungmin Park2d262c42008-11-19 16:26:54 +0100505 *
506 * This function takes care of physical eraseblock write failures. If write to
507 * the physical eraseblock write operation fails, the logical eraseblock is
508 * re-mapped to another physical eraseblock, the data is recovered, and the
509 * write finishes. UBI has a pool of reserved physical eraseblocks for this.
510 *
511 * If all the data were successfully written, zero is returned. If an error
512 * occurred and UBI has not been able to recover from it, this function returns
513 * a negative error code. Note, in case of an error, it is possible that
514 * something was still written to the flash media, but that may be some
515 * garbage.
516 *
517 * If the volume is damaged because of an interrupted update this function just
518 * returns immediately with %-EBADF code.
519 */
520int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200521 int offset, int len)
Kyungmin Park2d262c42008-11-19 16:26:54 +0100522{
523 struct ubi_volume *vol = desc->vol;
524 struct ubi_device *ubi = vol->ubi;
525 int vol_id = vol->vol_id;
526
Heiko Schocherff94bc42014-06-24 10:10:04 +0200527 dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100528
529 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
530 return -EINVAL;
531
532 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
533 return -EROFS;
534
535 if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
536 offset + len > vol->usable_leb_size ||
537 offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
538 return -EINVAL;
539
Kyungmin Park2d262c42008-11-19 16:26:54 +0100540 if (vol->upd_marker)
541 return -EBADF;
542
543 if (len == 0)
544 return 0;
545
Heiko Schocherff94bc42014-06-24 10:10:04 +0200546 return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100547}
548EXPORT_SYMBOL_GPL(ubi_leb_write);
549
550/*
551 * ubi_leb_change - change logical eraseblock atomically.
552 * @desc: volume descriptor
553 * @lnum: logical eraseblock number to change
554 * @buf: data to write
555 * @len: how many bytes to write
Kyungmin Park2d262c42008-11-19 16:26:54 +0100556 *
557 * This function changes the contents of a logical eraseblock atomically. @buf
558 * has to contain new logical eraseblock data, and @len - the length of the
Heiko Schocherff94bc42014-06-24 10:10:04 +0200559 * data, which has to be aligned. The length may be shorter than the logical
Kyungmin Park2d262c42008-11-19 16:26:54 +0100560 * eraseblock size, ant the logical eraseblock may be appended to more times
561 * later on. This function guarantees that in case of an unclean reboot the old
562 * contents is preserved. Returns zero in case of success and a negative error
563 * code in case of failure.
564 */
565int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200566 int len)
Kyungmin Park2d262c42008-11-19 16:26:54 +0100567{
568 struct ubi_volume *vol = desc->vol;
569 struct ubi_device *ubi = vol->ubi;
570 int vol_id = vol->vol_id;
571
Heiko Schocherff94bc42014-06-24 10:10:04 +0200572 dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100573
574 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
575 return -EINVAL;
576
577 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
578 return -EROFS;
579
580 if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
581 len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
582 return -EINVAL;
583
Kyungmin Park2d262c42008-11-19 16:26:54 +0100584 if (vol->upd_marker)
585 return -EBADF;
586
587 if (len == 0)
588 return 0;
589
Heiko Schocherff94bc42014-06-24 10:10:04 +0200590 return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100591}
592EXPORT_SYMBOL_GPL(ubi_leb_change);
593
594/**
595 * ubi_leb_erase - erase logical eraseblock.
596 * @desc: volume descriptor
597 * @lnum: logical eraseblock number
598 *
599 * This function un-maps logical eraseblock @lnum and synchronously erases the
600 * correspondent physical eraseblock. Returns zero in case of success and a
601 * negative error code in case of failure.
602 *
603 * If the volume is damaged because of an interrupted update this function just
604 * returns immediately with %-EBADF code.
605 */
606int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
607{
608 struct ubi_volume *vol = desc->vol;
609 struct ubi_device *ubi = vol->ubi;
610 int err;
611
Heiko Schocherff94bc42014-06-24 10:10:04 +0200612 dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100613
614 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
615 return -EROFS;
616
617 if (lnum < 0 || lnum >= vol->reserved_pebs)
618 return -EINVAL;
619
620 if (vol->upd_marker)
621 return -EBADF;
622
623 err = ubi_eba_unmap_leb(ubi, vol, lnum);
624 if (err)
625 return err;
626
Heiko Schocherff94bc42014-06-24 10:10:04 +0200627 return ubi_wl_flush(ubi, vol->vol_id, lnum);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100628}
629EXPORT_SYMBOL_GPL(ubi_leb_erase);
630
631/**
632 * ubi_leb_unmap - un-map logical eraseblock.
633 * @desc: volume descriptor
634 * @lnum: logical eraseblock number
635 *
636 * This function un-maps logical eraseblock @lnum and schedules the
637 * corresponding physical eraseblock for erasure, so that it will eventually be
Heiko Schocherff94bc42014-06-24 10:10:04 +0200638 * physically erased in background. This operation is much faster than the
Kyungmin Park2d262c42008-11-19 16:26:54 +0100639 * erase operation.
640 *
641 * Unlike erase, the un-map operation does not guarantee that the logical
642 * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
643 * example, if several logical eraseblocks are un-mapped, and an unclean reboot
644 * happens after this, the logical eraseblocks will not necessarily be
645 * un-mapped again when this MTD device is attached. They may actually be
646 * mapped to the same physical eraseblocks again. So, this function has to be
647 * used with care.
648 *
649 * In other words, when un-mapping a logical eraseblock, UBI does not store
650 * any information about this on the flash media, it just marks the logical
651 * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
652 * eraseblock is physically erased, it will be mapped again to the same logical
653 * eraseblock when the MTD device is attached again.
654 *
655 * The main and obvious use-case of this function is when the contents of a
656 * logical eraseblock has to be re-written. Then it is much more efficient to
Heiko Schocherff94bc42014-06-24 10:10:04 +0200657 * first un-map it, then write new data, rather than first erase it, then write
Kyungmin Park2d262c42008-11-19 16:26:54 +0100658 * new data. Note, once new data has been written to the logical eraseblock,
659 * UBI guarantees that the old contents has gone forever. In other words, if an
660 * unclean reboot happens after the logical eraseblock has been un-mapped and
661 * then written to, it will contain the last written data.
662 *
663 * This function returns zero in case of success and a negative error code in
664 * case of failure. If the volume is damaged because of an interrupted update
665 * this function just returns immediately with %-EBADF code.
666 */
667int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
668{
669 struct ubi_volume *vol = desc->vol;
670 struct ubi_device *ubi = vol->ubi;
671
Heiko Schocherff94bc42014-06-24 10:10:04 +0200672 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100673
674 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
675 return -EROFS;
676
677 if (lnum < 0 || lnum >= vol->reserved_pebs)
678 return -EINVAL;
679
680 if (vol->upd_marker)
681 return -EBADF;
682
683 return ubi_eba_unmap_leb(ubi, vol, lnum);
684}
685EXPORT_SYMBOL_GPL(ubi_leb_unmap);
686
687/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200688 * ubi_leb_map - map logical eraseblock to a physical eraseblock.
Kyungmin Park2d262c42008-11-19 16:26:54 +0100689 * @desc: volume descriptor
690 * @lnum: logical eraseblock number
Kyungmin Park2d262c42008-11-19 16:26:54 +0100691 *
692 * This function maps an un-mapped logical eraseblock @lnum to a physical
Heiko Schocherff94bc42014-06-24 10:10:04 +0200693 * eraseblock. This means, that after a successful invocation of this
Kyungmin Park2d262c42008-11-19 16:26:54 +0100694 * function the logical eraseblock @lnum will be empty (contain only %0xFF
695 * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
696 * happens.
697 *
698 * This function returns zero in case of success, %-EBADF if the volume is
699 * damaged because of an interrupted update, %-EBADMSG if the logical
700 * eraseblock is already mapped, and other negative error codes in case of
701 * other failures.
702 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200703int ubi_leb_map(struct ubi_volume_desc *desc, int lnum)
Kyungmin Park2d262c42008-11-19 16:26:54 +0100704{
705 struct ubi_volume *vol = desc->vol;
706 struct ubi_device *ubi = vol->ubi;
707
Heiko Schocherff94bc42014-06-24 10:10:04 +0200708 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100709
710 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
711 return -EROFS;
712
713 if (lnum < 0 || lnum >= vol->reserved_pebs)
714 return -EINVAL;
715
Kyungmin Park2d262c42008-11-19 16:26:54 +0100716 if (vol->upd_marker)
717 return -EBADF;
718
719 if (vol->eba_tbl[lnum] >= 0)
720 return -EBADMSG;
721
Heiko Schocherff94bc42014-06-24 10:10:04 +0200722 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100723}
724EXPORT_SYMBOL_GPL(ubi_leb_map);
725
726/**
727 * ubi_is_mapped - check if logical eraseblock is mapped.
728 * @desc: volume descriptor
729 * @lnum: logical eraseblock number
730 *
731 * This function checks if logical eraseblock @lnum is mapped to a physical
732 * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
733 * mean it will still be un-mapped after the UBI device is re-attached. The
734 * logical eraseblock may become mapped to the physical eraseblock it was last
735 * mapped to.
736 *
737 * This function returns %1 if the LEB is mapped, %0 if not, and a negative
738 * error code in case of failure. If the volume is damaged because of an
739 * interrupted update this function just returns immediately with %-EBADF error
740 * code.
741 */
742int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
743{
744 struct ubi_volume *vol = desc->vol;
745
Heiko Schocherff94bc42014-06-24 10:10:04 +0200746 dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100747
748 if (lnum < 0 || lnum >= vol->reserved_pebs)
749 return -EINVAL;
750
751 if (vol->upd_marker)
752 return -EBADF;
753
754 return vol->eba_tbl[lnum] >= 0;
755}
756EXPORT_SYMBOL_GPL(ubi_is_mapped);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200757
758/**
759 * ubi_sync - synchronize UBI device buffers.
760 * @ubi_num: UBI device to synchronize
761 *
762 * The underlying MTD device may cache data in hardware or in software. This
763 * function ensures the caches are flushed. Returns zero in case of success and
764 * a negative error code in case of failure.
765 */
766int ubi_sync(int ubi_num)
767{
768 struct ubi_device *ubi;
769
770 ubi = ubi_get_device(ubi_num);
771 if (!ubi)
772 return -ENODEV;
773
774 mtd_sync(ubi->mtd);
775 ubi_put_device(ubi);
776 return 0;
777}
778EXPORT_SYMBOL_GPL(ubi_sync);
779
780/**
781 * ubi_flush - flush UBI work queue.
782 * @ubi_num: UBI device to flush work queue
783 * @vol_id: volume id to flush for
784 * @lnum: logical eraseblock number to flush for
785 *
786 * This function executes all pending works for a particular volume id / logical
787 * eraseblock number pair. If either value is set to %UBI_ALL, then it acts as
788 * a wildcard for all of the corresponding volume numbers or logical
789 * eraseblock numbers. It returns zero in case of success and a negative error
790 * code in case of failure.
791 */
792int ubi_flush(int ubi_num, int vol_id, int lnum)
793{
794 struct ubi_device *ubi;
795 int err = 0;
796
797 ubi = ubi_get_device(ubi_num);
798 if (!ubi)
799 return -ENODEV;
800
801 err = ubi_wl_flush(ubi, vol_id, lnum);
802 ubi_put_device(ubi);
803 return err;
804}
805EXPORT_SYMBOL_GPL(ubi_flush);
806
807#ifndef __UBOOT__
808BLOCKING_NOTIFIER_HEAD(ubi_notifiers);
809
810/**
811 * ubi_register_volume_notifier - register a volume notifier.
812 * @nb: the notifier description object
813 * @ignore_existing: if non-zero, do not send "added" notification for all
814 * already existing volumes
815 *
816 * This function registers a volume notifier, which means that
817 * 'nb->notifier_call()' will be invoked when an UBI volume is created,
818 * removed, re-sized, re-named, or updated. The first argument of the function
819 * is the notification type. The second argument is pointer to a
820 * &struct ubi_notification object which describes the notification event.
821 * Using UBI API from the volume notifier is prohibited.
822 *
823 * This function returns zero in case of success and a negative error code
824 * in case of failure.
825 */
826int ubi_register_volume_notifier(struct notifier_block *nb,
827 int ignore_existing)
828{
829 int err;
830
831 err = blocking_notifier_chain_register(&ubi_notifiers, nb);
832 if (err != 0)
833 return err;
834 if (ignore_existing)
835 return 0;
836
837 /*
838 * We are going to walk all UBI devices and all volumes, and
839 * notify the user about existing volumes by the %UBI_VOLUME_ADDED
840 * event. We have to lock the @ubi_devices_mutex to make sure UBI
841 * devices do not disappear.
842 */
843 mutex_lock(&ubi_devices_mutex);
844 ubi_enumerate_volumes(nb);
845 mutex_unlock(&ubi_devices_mutex);
846
847 return err;
848}
849EXPORT_SYMBOL_GPL(ubi_register_volume_notifier);
850
851/**
852 * ubi_unregister_volume_notifier - unregister the volume notifier.
853 * @nb: the notifier description object
854 *
855 * This function unregisters volume notifier @nm and returns zero in case of
856 * success and a negative error code in case of failure.
857 */
858int ubi_unregister_volume_notifier(struct notifier_block *nb)
859{
860 return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
861}
862EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);
863#endif