xref: /openbmc/linux/drivers/mtd/ubi/kapi.c (revision b627b4ed)
1 /*
2  * Copyright (c) International Business Machines Corp., 2006
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12  * the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Artem Bityutskiy (Битюцкий Артём)
19  */
20 
21 /* This file mostly implements UBI kernel API functions */
22 
23 #include <linux/module.h>
24 #include <linux/err.h>
25 #include <asm/div64.h>
26 #include "ubi.h"
27 
28 /**
29  * ubi_get_device_info - get information about UBI device.
30  * @ubi_num: UBI device number
31  * @di: the information is stored here
32  *
33  * This function returns %0 in case of success, %-EINVAL if the UBI device
34  * number is invalid, and %-ENODEV if there is no such UBI device.
35  */
36 int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
37 {
38 	struct ubi_device *ubi;
39 
40 	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
41 		return -EINVAL;
42 
43 	ubi = ubi_get_device(ubi_num);
44 	if (!ubi)
45 		return -ENODEV;
46 
47 	di->ubi_num = ubi->ubi_num;
48 	di->leb_size = ubi->leb_size;
49 	di->min_io_size = ubi->min_io_size;
50 	di->ro_mode = ubi->ro_mode;
51 	di->cdev = ubi->cdev.dev;
52 
53 	ubi_put_device(ubi);
54 	return 0;
55 }
56 EXPORT_SYMBOL_GPL(ubi_get_device_info);
57 
58 /**
59  * ubi_get_volume_info - get information about UBI volume.
60  * @desc: volume descriptor
61  * @vi: the information is stored here
62  */
63 void ubi_get_volume_info(struct ubi_volume_desc *desc,
64 			 struct ubi_volume_info *vi)
65 {
66 	const struct ubi_volume *vol = desc->vol;
67 	const struct ubi_device *ubi = vol->ubi;
68 
69 	vi->vol_id = vol->vol_id;
70 	vi->ubi_num = ubi->ubi_num;
71 	vi->size = vol->reserved_pebs;
72 	vi->used_bytes = vol->used_bytes;
73 	vi->vol_type = vol->vol_type;
74 	vi->corrupted = vol->corrupted;
75 	vi->upd_marker = vol->upd_marker;
76 	vi->alignment = vol->alignment;
77 	vi->usable_leb_size = vol->usable_leb_size;
78 	vi->name_len = vol->name_len;
79 	vi->name = vol->name;
80 	vi->cdev = vol->cdev.dev;
81 }
82 EXPORT_SYMBOL_GPL(ubi_get_volume_info);
83 
84 /**
85  * ubi_open_volume - open UBI volume.
86  * @ubi_num: UBI device number
87  * @vol_id: volume ID
88  * @mode: open mode
89  *
90  * The @mode parameter specifies if the volume should be opened in read-only
91  * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
92  * nobody else will be able to open this volume. UBI allows to have many volume
93  * readers and one writer at a time.
94  *
95  * If a static volume is being opened for the first time since boot, it will be
96  * checked by this function, which means it will be fully read and the CRC
97  * checksum of each logical eraseblock will be checked.
98  *
99  * This function returns volume descriptor in case of success and a negative
100  * error code in case of failure.
101  */
102 struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
103 {
104 	int err;
105 	struct ubi_volume_desc *desc;
106 	struct ubi_device *ubi;
107 	struct ubi_volume *vol;
108 
109 	dbg_gen("open device %d volume %d, mode %d", ubi_num, vol_id, mode);
110 
111 	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
112 		return ERR_PTR(-EINVAL);
113 
114 	if (mode != UBI_READONLY && mode != UBI_READWRITE &&
115 	    mode != UBI_EXCLUSIVE)
116 		return ERR_PTR(-EINVAL);
117 
118 	/*
119 	 * First of all, we have to get the UBI device to prevent its removal.
120 	 */
121 	ubi = ubi_get_device(ubi_num);
122 	if (!ubi)
123 		return ERR_PTR(-ENODEV);
124 
125 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots) {
126 		err = -EINVAL;
127 		goto out_put_ubi;
128 	}
129 
130 	desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
131 	if (!desc) {
132 		err = -ENOMEM;
133 		goto out_put_ubi;
134 	}
135 
136 	err = -ENODEV;
137 	if (!try_module_get(THIS_MODULE))
138 		goto out_free;
139 
140 	spin_lock(&ubi->volumes_lock);
141 	vol = ubi->volumes[vol_id];
142 	if (!vol)
143 		goto out_unlock;
144 
145 	err = -EBUSY;
146 	switch (mode) {
147 	case UBI_READONLY:
148 		if (vol->exclusive)
149 			goto out_unlock;
150 		vol->readers += 1;
151 		break;
152 
153 	case UBI_READWRITE:
154 		if (vol->exclusive || vol->writers > 0)
155 			goto out_unlock;
156 		vol->writers += 1;
157 		break;
158 
159 	case UBI_EXCLUSIVE:
160 		if (vol->exclusive || vol->writers || vol->readers)
161 			goto out_unlock;
162 		vol->exclusive = 1;
163 		break;
164 	}
165 	get_device(&vol->dev);
166 	vol->ref_count += 1;
167 	spin_unlock(&ubi->volumes_lock);
168 
169 	desc->vol = vol;
170 	desc->mode = mode;
171 
172 	mutex_lock(&ubi->ckvol_mutex);
173 	if (!vol->checked) {
174 		/* This is the first open - check the volume */
175 		err = ubi_check_volume(ubi, vol_id);
176 		if (err < 0) {
177 			mutex_unlock(&ubi->ckvol_mutex);
178 			ubi_close_volume(desc);
179 			return ERR_PTR(err);
180 		}
181 		if (err == 1) {
182 			ubi_warn("volume %d on UBI device %d is corrupted",
183 				 vol_id, ubi->ubi_num);
184 			vol->corrupted = 1;
185 		}
186 		vol->checked = 1;
187 	}
188 	mutex_unlock(&ubi->ckvol_mutex);
189 
190 	return desc;
191 
192 out_unlock:
193 	spin_unlock(&ubi->volumes_lock);
194 	module_put(THIS_MODULE);
195 out_free:
196 	kfree(desc);
197 out_put_ubi:
198 	ubi_put_device(ubi);
199 	return ERR_PTR(err);
200 }
201 EXPORT_SYMBOL_GPL(ubi_open_volume);
202 
203 /**
204  * ubi_open_volume_nm - open UBI volume by name.
205  * @ubi_num: UBI device number
206  * @name: volume name
207  * @mode: open mode
208  *
209  * This function is similar to 'ubi_open_volume()', but opens a volume by name.
210  */
211 struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
212 					   int mode)
213 {
214 	int i, vol_id = -1, len;
215 	struct ubi_device *ubi;
216 	struct ubi_volume_desc *ret;
217 
218 	dbg_gen("open volume %s, mode %d", name, mode);
219 
220 	if (!name)
221 		return ERR_PTR(-EINVAL);
222 
223 	len = strnlen(name, UBI_VOL_NAME_MAX + 1);
224 	if (len > UBI_VOL_NAME_MAX)
225 		return ERR_PTR(-EINVAL);
226 
227 	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
228 		return ERR_PTR(-EINVAL);
229 
230 	ubi = ubi_get_device(ubi_num);
231 	if (!ubi)
232 		return ERR_PTR(-ENODEV);
233 
234 	spin_lock(&ubi->volumes_lock);
235 	/* Walk all volumes of this UBI device */
236 	for (i = 0; i < ubi->vtbl_slots; i++) {
237 		struct ubi_volume *vol = ubi->volumes[i];
238 
239 		if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
240 			vol_id = i;
241 			break;
242 		}
243 	}
244 	spin_unlock(&ubi->volumes_lock);
245 
246 	if (vol_id >= 0)
247 		ret = ubi_open_volume(ubi_num, vol_id, mode);
248 	else
249 		ret = ERR_PTR(-ENODEV);
250 
251 	/*
252 	 * We should put the UBI device even in case of success, because
253 	 * 'ubi_open_volume()' took a reference as well.
254 	 */
255 	ubi_put_device(ubi);
256 	return ret;
257 }
258 EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
259 
260 /**
261  * ubi_close_volume - close UBI volume.
262  * @desc: volume descriptor
263  */
264 void ubi_close_volume(struct ubi_volume_desc *desc)
265 {
266 	struct ubi_volume *vol = desc->vol;
267 	struct ubi_device *ubi = vol->ubi;
268 
269 	dbg_gen("close volume %d, mode %d", vol->vol_id, desc->mode);
270 
271 	spin_lock(&ubi->volumes_lock);
272 	switch (desc->mode) {
273 	case UBI_READONLY:
274 		vol->readers -= 1;
275 		break;
276 	case UBI_READWRITE:
277 		vol->writers -= 1;
278 		break;
279 	case UBI_EXCLUSIVE:
280 		vol->exclusive = 0;
281 	}
282 	vol->ref_count -= 1;
283 	spin_unlock(&ubi->volumes_lock);
284 
285 	kfree(desc);
286 	put_device(&vol->dev);
287 	ubi_put_device(ubi);
288 	module_put(THIS_MODULE);
289 }
290 EXPORT_SYMBOL_GPL(ubi_close_volume);
291 
292 /**
293  * ubi_leb_read - read data.
294  * @desc: volume descriptor
295  * @lnum: logical eraseblock number to read from
296  * @buf: buffer where to store the read data
297  * @offset: offset within the logical eraseblock to read from
298  * @len: how many bytes to read
299  * @check: whether UBI has to check the read data's CRC or not.
300  *
301  * This function reads data from offset @offset of logical eraseblock @lnum and
302  * stores the data at @buf. When reading from static volumes, @check specifies
303  * whether the data has to be checked or not. If yes, the whole logical
304  * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
305  * checksum is per-eraseblock). So checking may substantially slow down the
306  * read speed. The @check argument is ignored for dynamic volumes.
307  *
308  * In case of success, this function returns zero. In case of failure, this
309  * function returns a negative error code.
310  *
311  * %-EBADMSG error code is returned:
312  * o for both static and dynamic volumes if MTD driver has detected a data
313  *   integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
314  * o for static volumes in case of data CRC mismatch.
315  *
316  * If the volume is damaged because of an interrupted update this function just
317  * returns immediately with %-EBADF error code.
318  */
319 int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
320 		 int len, int check)
321 {
322 	struct ubi_volume *vol = desc->vol;
323 	struct ubi_device *ubi = vol->ubi;
324 	int err, vol_id = vol->vol_id;
325 
326 	dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
327 
328 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
329 	    lnum >= vol->used_ebs || offset < 0 || len < 0 ||
330 	    offset + len > vol->usable_leb_size)
331 		return -EINVAL;
332 
333 	if (vol->vol_type == UBI_STATIC_VOLUME) {
334 		if (vol->used_ebs == 0)
335 			/* Empty static UBI volume */
336 			return 0;
337 		if (lnum == vol->used_ebs - 1 &&
338 		    offset + len > vol->last_eb_bytes)
339 			return -EINVAL;
340 	}
341 
342 	if (vol->upd_marker)
343 		return -EBADF;
344 	if (len == 0)
345 		return 0;
346 
347 	err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
348 	if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) {
349 		ubi_warn("mark volume %d as corrupted", vol_id);
350 		vol->corrupted = 1;
351 	}
352 
353 	return err;
354 }
355 EXPORT_SYMBOL_GPL(ubi_leb_read);
356 
357 /**
358  * ubi_leb_write - write data.
359  * @desc: volume descriptor
360  * @lnum: logical eraseblock number to write to
361  * @buf: data to write
362  * @offset: offset within the logical eraseblock where to write
363  * @len: how many bytes to write
364  * @dtype: expected data type
365  *
366  * This function writes @len bytes of data from @buf to offset @offset of
367  * logical eraseblock @lnum. The @dtype argument describes expected lifetime of
368  * the data.
369  *
370  * This function takes care of physical eraseblock write failures. If write to
371  * the physical eraseblock write operation fails, the logical eraseblock is
372  * re-mapped to another physical eraseblock, the data is recovered, and the
373  * write finishes. UBI has a pool of reserved physical eraseblocks for this.
374  *
375  * If all the data were successfully written, zero is returned. If an error
376  * occurred and UBI has not been able to recover from it, this function returns
377  * a negative error code. Note, in case of an error, it is possible that
378  * something was still written to the flash media, but that may be some
379  * garbage.
380  *
381  * If the volume is damaged because of an interrupted update this function just
382  * returns immediately with %-EBADF code.
383  */
384 int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
385 		  int offset, int len, int dtype)
386 {
387 	struct ubi_volume *vol = desc->vol;
388 	struct ubi_device *ubi = vol->ubi;
389 	int vol_id = vol->vol_id;
390 
391 	dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
392 
393 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
394 		return -EINVAL;
395 
396 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
397 		return -EROFS;
398 
399 	if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
400 	    offset + len > vol->usable_leb_size ||
401 	    offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
402 		return -EINVAL;
403 
404 	if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
405 	    dtype != UBI_UNKNOWN)
406 		return -EINVAL;
407 
408 	if (vol->upd_marker)
409 		return -EBADF;
410 
411 	if (len == 0)
412 		return 0;
413 
414 	return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype);
415 }
416 EXPORT_SYMBOL_GPL(ubi_leb_write);
417 
418 /*
419  * ubi_leb_change - change logical eraseblock atomically.
420  * @desc: volume descriptor
421  * @lnum: logical eraseblock number to change
422  * @buf: data to write
423  * @len: how many bytes to write
424  * @dtype: expected data type
425  *
426  * This function changes the contents of a logical eraseblock atomically. @buf
427  * has to contain new logical eraseblock data, and @len - the length of the
428  * data, which has to be aligned. The length may be shorter then the logical
429  * eraseblock size, ant the logical eraseblock may be appended to more times
430  * later on. This function guarantees that in case of an unclean reboot the old
431  * contents is preserved. Returns zero in case of success and a negative error
432  * code in case of failure.
433  */
434 int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
435 		   int len, int dtype)
436 {
437 	struct ubi_volume *vol = desc->vol;
438 	struct ubi_device *ubi = vol->ubi;
439 	int vol_id = vol->vol_id;
440 
441 	dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
442 
443 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
444 		return -EINVAL;
445 
446 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
447 		return -EROFS;
448 
449 	if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
450 	    len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
451 		return -EINVAL;
452 
453 	if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
454 	    dtype != UBI_UNKNOWN)
455 		return -EINVAL;
456 
457 	if (vol->upd_marker)
458 		return -EBADF;
459 
460 	if (len == 0)
461 		return 0;
462 
463 	return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype);
464 }
465 EXPORT_SYMBOL_GPL(ubi_leb_change);
466 
467 /**
468  * ubi_leb_erase - erase logical eraseblock.
469  * @desc: volume descriptor
470  * @lnum: logical eraseblock number
471  *
472  * This function un-maps logical eraseblock @lnum and synchronously erases the
473  * correspondent physical eraseblock. Returns zero in case of success and a
474  * negative error code in case of failure.
475  *
476  * If the volume is damaged because of an interrupted update this function just
477  * returns immediately with %-EBADF code.
478  */
479 int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
480 {
481 	struct ubi_volume *vol = desc->vol;
482 	struct ubi_device *ubi = vol->ubi;
483 	int err;
484 
485 	dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
486 
487 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
488 		return -EROFS;
489 
490 	if (lnum < 0 || lnum >= vol->reserved_pebs)
491 		return -EINVAL;
492 
493 	if (vol->upd_marker)
494 		return -EBADF;
495 
496 	err = ubi_eba_unmap_leb(ubi, vol, lnum);
497 	if (err)
498 		return err;
499 
500 	return ubi_wl_flush(ubi);
501 }
502 EXPORT_SYMBOL_GPL(ubi_leb_erase);
503 
504 /**
505  * ubi_leb_unmap - un-map logical eraseblock.
506  * @desc: volume descriptor
507  * @lnum: logical eraseblock number
508  *
509  * This function un-maps logical eraseblock @lnum and schedules the
510  * corresponding physical eraseblock for erasure, so that it will eventually be
511  * physically erased in background. This operation is much faster then the
512  * erase operation.
513  *
514  * Unlike erase, the un-map operation does not guarantee that the logical
515  * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
516  * example, if several logical eraseblocks are un-mapped, and an unclean reboot
517  * happens after this, the logical eraseblocks will not necessarily be
518  * un-mapped again when this MTD device is attached. They may actually be
519  * mapped to the same physical eraseblocks again. So, this function has to be
520  * used with care.
521  *
522  * In other words, when un-mapping a logical eraseblock, UBI does not store
523  * any information about this on the flash media, it just marks the logical
524  * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
525  * eraseblock is physically erased, it will be mapped again to the same logical
526  * eraseblock when the MTD device is attached again.
527  *
528  * The main and obvious use-case of this function is when the contents of a
529  * logical eraseblock has to be re-written. Then it is much more efficient to
530  * first un-map it, then write new data, rather then first erase it, then write
531  * new data. Note, once new data has been written to the logical eraseblock,
532  * UBI guarantees that the old contents has gone forever. In other words, if an
533  * unclean reboot happens after the logical eraseblock has been un-mapped and
534  * then written to, it will contain the last written data.
535  *
536  * This function returns zero in case of success and a negative error code in
537  * case of failure. If the volume is damaged because of an interrupted update
538  * this function just returns immediately with %-EBADF code.
539  */
540 int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
541 {
542 	struct ubi_volume *vol = desc->vol;
543 	struct ubi_device *ubi = vol->ubi;
544 
545 	dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
546 
547 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
548 		return -EROFS;
549 
550 	if (lnum < 0 || lnum >= vol->reserved_pebs)
551 		return -EINVAL;
552 
553 	if (vol->upd_marker)
554 		return -EBADF;
555 
556 	return ubi_eba_unmap_leb(ubi, vol, lnum);
557 }
558 EXPORT_SYMBOL_GPL(ubi_leb_unmap);
559 
560 /**
561  * ubi_leb_map - map logical erasblock to a physical eraseblock.
562  * @desc: volume descriptor
563  * @lnum: logical eraseblock number
564  * @dtype: expected data type
565  *
566  * This function maps an un-mapped logical eraseblock @lnum to a physical
567  * eraseblock. This means, that after a successful invocation of this
568  * function the logical eraseblock @lnum will be empty (contain only %0xFF
569  * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
570  * happens.
571  *
572  * This function returns zero in case of success, %-EBADF if the volume is
573  * damaged because of an interrupted update, %-EBADMSG if the logical
574  * eraseblock is already mapped, and other negative error codes in case of
575  * other failures.
576  */
577 int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
578 {
579 	struct ubi_volume *vol = desc->vol;
580 	struct ubi_device *ubi = vol->ubi;
581 
582 	dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
583 
584 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
585 		return -EROFS;
586 
587 	if (lnum < 0 || lnum >= vol->reserved_pebs)
588 		return -EINVAL;
589 
590 	if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
591 	    dtype != UBI_UNKNOWN)
592 		return -EINVAL;
593 
594 	if (vol->upd_marker)
595 		return -EBADF;
596 
597 	if (vol->eba_tbl[lnum] >= 0)
598 		return -EBADMSG;
599 
600 	return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
601 }
602 EXPORT_SYMBOL_GPL(ubi_leb_map);
603 
604 /**
605  * ubi_is_mapped - check if logical eraseblock is mapped.
606  * @desc: volume descriptor
607  * @lnum: logical eraseblock number
608  *
609  * This function checks if logical eraseblock @lnum is mapped to a physical
610  * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
611  * mean it will still be un-mapped after the UBI device is re-attached. The
612  * logical eraseblock may become mapped to the physical eraseblock it was last
613  * mapped to.
614  *
615  * This function returns %1 if the LEB is mapped, %0 if not, and a negative
616  * error code in case of failure. If the volume is damaged because of an
617  * interrupted update this function just returns immediately with %-EBADF error
618  * code.
619  */
620 int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
621 {
622 	struct ubi_volume *vol = desc->vol;
623 
624 	dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
625 
626 	if (lnum < 0 || lnum >= vol->reserved_pebs)
627 		return -EINVAL;
628 
629 	if (vol->upd_marker)
630 		return -EBADF;
631 
632 	return vol->eba_tbl[lnum] >= 0;
633 }
634 EXPORT_SYMBOL_GPL(ubi_is_mapped);
635 
636 /**
637  * ubi_sync - synchronize UBI device buffers.
638  * @ubi_num: UBI device to synchronize
639  *
640  * The underlying MTD device may cache data in hardware or in software. This
641  * function ensures the caches are flushed. Returns zero in case of success and
642  * a negative error code in case of failure.
643  */
644 int ubi_sync(int ubi_num)
645 {
646 	struct ubi_device *ubi;
647 
648 	ubi = ubi_get_device(ubi_num);
649 	if (!ubi)
650 		return -ENODEV;
651 
652 	if (ubi->mtd->sync)
653 		ubi->mtd->sync(ubi->mtd);
654 
655 	ubi_put_device(ubi);
656 	return 0;
657 }
658 EXPORT_SYMBOL_GPL(ubi_sync);
659