1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/module.h>
24#include <linux/err.h>
25#include <linux/slab.h>
26#include <linux/namei.h>
27#include <linux/fs.h>
28#include <asm/div64.h>
29#include "ubi.h"
30
31
32
33
34
35
36
37
38
39void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di)
40{
41 di->ubi_num = ubi->ubi_num;
42 di->leb_size = ubi->leb_size;
43 di->leb_start = ubi->leb_start;
44 di->min_io_size = ubi->min_io_size;
45 di->max_write_size = ubi->max_write_size;
46 di->ro_mode = ubi->ro_mode;
47 di->cdev = ubi->cdev.dev;
48}
49EXPORT_SYMBOL_GPL(ubi_do_get_device_info);
50
51
52
53
54
55
56
57
58
59int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
60{
61 struct ubi_device *ubi;
62
63 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
64 return -EINVAL;
65 ubi = ubi_get_device(ubi_num);
66 if (!ubi)
67 return -ENODEV;
68 ubi_do_get_device_info(ubi, di);
69 ubi_put_device(ubi);
70 return 0;
71}
72EXPORT_SYMBOL_GPL(ubi_get_device_info);
73
74
75
76
77
78
79
80void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
81 struct ubi_volume_info *vi)
82{
83 vi->vol_id = vol->vol_id;
84 vi->ubi_num = ubi->ubi_num;
85 vi->size = vol->reserved_pebs;
86 vi->used_bytes = vol->used_bytes;
87 vi->vol_type = vol->vol_type;
88 vi->corrupted = vol->corrupted;
89 vi->upd_marker = vol->upd_marker;
90 vi->alignment = vol->alignment;
91 vi->usable_leb_size = vol->usable_leb_size;
92 vi->name_len = vol->name_len;
93 vi->name = vol->name;
94 vi->cdev = vol->cdev.dev;
95}
96
97
98
99
100
101
102void ubi_get_volume_info(struct ubi_volume_desc *desc,
103 struct ubi_volume_info *vi)
104{
105 ubi_do_get_volume_info(desc->vol->ubi, desc->vol, vi);
106}
107EXPORT_SYMBOL_GPL(ubi_get_volume_info);
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
128{
129 int err;
130 struct ubi_volume_desc *desc;
131 struct ubi_device *ubi;
132 struct ubi_volume *vol;
133
134 dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode);
135
136 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
137 return ERR_PTR(-EINVAL);
138
139 if (mode != UBI_READONLY && mode != UBI_READWRITE &&
140 mode != UBI_EXCLUSIVE && mode != UBI_METAONLY)
141 return ERR_PTR(-EINVAL);
142
143
144
145
146 ubi = ubi_get_device(ubi_num);
147 if (!ubi)
148 return ERR_PTR(-ENODEV);
149
150 if (vol_id < 0 || vol_id >= ubi->vtbl_slots) {
151 err = -EINVAL;
152 goto out_put_ubi;
153 }
154
155 desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
156 if (!desc) {
157 err = -ENOMEM;
158 goto out_put_ubi;
159 }
160
161 err = -ENODEV;
162 if (!try_module_get(THIS_MODULE))
163 goto out_free;
164
165 spin_lock(&ubi->volumes_lock);
166 vol = ubi->volumes[vol_id];
167 if (!vol)
168 goto out_unlock;
169
170 err = -EBUSY;
171 switch (mode) {
172 case UBI_READONLY:
173 if (vol->exclusive)
174 goto out_unlock;
175 vol->readers += 1;
176 break;
177
178 case UBI_READWRITE:
179 if (vol->exclusive || vol->writers > 0)
180 goto out_unlock;
181 vol->writers += 1;
182 break;
183
184 case UBI_EXCLUSIVE:
185 if (vol->exclusive || vol->writers || vol->readers ||
186 vol->metaonly)
187 goto out_unlock;
188 vol->exclusive = 1;
189 break;
190
191 case UBI_METAONLY:
192 if (vol->metaonly || vol->exclusive)
193 goto out_unlock;
194 vol->metaonly = 1;
195 break;
196 }
197 get_device(&vol->dev);
198 vol->ref_count += 1;
199 spin_unlock(&ubi->volumes_lock);
200
201 desc->vol = vol;
202 desc->mode = mode;
203
204 mutex_lock(&ubi->ckvol_mutex);
205 if (!vol->checked) {
206
207 err = ubi_check_volume(ubi, vol_id);
208 if (err < 0) {
209 mutex_unlock(&ubi->ckvol_mutex);
210 ubi_close_volume(desc);
211 return ERR_PTR(err);
212 }
213 if (err == 1) {
214 ubi_warn(ubi, "volume %d on UBI device %d is corrupted",
215 vol_id, ubi->ubi_num);
216 vol->corrupted = 1;
217 }
218 vol->checked = 1;
219 }
220 mutex_unlock(&ubi->ckvol_mutex);
221
222 return desc;
223
224out_unlock:
225 spin_unlock(&ubi->volumes_lock);
226 module_put(THIS_MODULE);
227out_free:
228 kfree(desc);
229out_put_ubi:
230 ubi_put_device(ubi);
231 ubi_err(ubi, "cannot open device %d, volume %d, error %d",
232 ubi_num, vol_id, err);
233 return ERR_PTR(err);
234}
235EXPORT_SYMBOL_GPL(ubi_open_volume);
236
237
238
239
240
241
242
243
244
245struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
246 int mode)
247{
248 int i, vol_id = -1, len;
249 struct ubi_device *ubi;
250 struct ubi_volume_desc *ret;
251
252 dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode);
253
254 if (!name)
255 return ERR_PTR(-EINVAL);
256
257 len = strnlen(name, UBI_VOL_NAME_MAX + 1);
258 if (len > UBI_VOL_NAME_MAX)
259 return ERR_PTR(-EINVAL);
260
261 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
262 return ERR_PTR(-EINVAL);
263
264 ubi = ubi_get_device(ubi_num);
265 if (!ubi)
266 return ERR_PTR(-ENODEV);
267
268 spin_lock(&ubi->volumes_lock);
269
270 for (i = 0; i < ubi->vtbl_slots; i++) {
271 struct ubi_volume *vol = ubi->volumes[i];
272
273 if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
274 vol_id = i;
275 break;
276 }
277 }
278 spin_unlock(&ubi->volumes_lock);
279
280 if (vol_id >= 0)
281 ret = ubi_open_volume(ubi_num, vol_id, mode);
282 else
283 ret = ERR_PTR(-ENODEV);
284
285
286
287
288
289 ubi_put_device(ubi);
290 return ret;
291}
292EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
293
294
295
296
297
298
299
300
301
302struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode)
303{
304 int error, ubi_num, vol_id, mod;
305 struct inode *inode;
306 struct path path;
307
308 dbg_gen("open volume %s, mode %d", pathname, mode);
309
310 if (!pathname || !*pathname)
311 return ERR_PTR(-EINVAL);
312
313 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
314 if (error)
315 return ERR_PTR(error);
316
317 inode = d_backing_inode(path.dentry);
318 mod = inode->i_mode;
319 ubi_num = ubi_major2num(imajor(inode));
320 vol_id = iminor(inode) - 1;
321 path_put(&path);
322
323 if (!S_ISCHR(mod))
324 return ERR_PTR(-EINVAL);
325 if (vol_id >= 0 && ubi_num >= 0)
326 return ubi_open_volume(ubi_num, vol_id, mode);
327 return ERR_PTR(-ENODEV);
328}
329EXPORT_SYMBOL_GPL(ubi_open_volume_path);
330
331
332
333
334
335void ubi_close_volume(struct ubi_volume_desc *desc)
336{
337 struct ubi_volume *vol = desc->vol;
338 struct ubi_device *ubi = vol->ubi;
339
340 dbg_gen("close device %d, volume %d, mode %d",
341 ubi->ubi_num, vol->vol_id, desc->mode);
342
343 spin_lock(&ubi->volumes_lock);
344 switch (desc->mode) {
345 case UBI_READONLY:
346 vol->readers -= 1;
347 break;
348 case UBI_READWRITE:
349 vol->writers -= 1;
350 break;
351 case UBI_EXCLUSIVE:
352 vol->exclusive = 0;
353 break;
354 case UBI_METAONLY:
355 vol->metaonly = 0;
356 break;
357 }
358 vol->ref_count -= 1;
359 spin_unlock(&ubi->volumes_lock);
360
361 kfree(desc);
362 put_device(&vol->dev);
363 ubi_put_device(ubi);
364 module_put(THIS_MODULE);
365}
366EXPORT_SYMBOL_GPL(ubi_close_volume);
367
368
369
370
371
372
373
374
375
376
377
378static int leb_read_sanity_check(struct ubi_volume_desc *desc, int lnum,
379 int offset, int len)
380{
381 struct ubi_volume *vol = desc->vol;
382 struct ubi_device *ubi = vol->ubi;
383 int vol_id = vol->vol_id;
384
385 if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
386 lnum >= vol->used_ebs || offset < 0 || len < 0 ||
387 offset + len > vol->usable_leb_size)
388 return -EINVAL;
389
390 if (vol->vol_type == UBI_STATIC_VOLUME) {
391 if (vol->used_ebs == 0)
392
393 return 0;
394 if (lnum == vol->used_ebs - 1 &&
395 offset + len > vol->last_eb_bytes)
396 return -EINVAL;
397 }
398
399 if (vol->upd_marker)
400 return -EBADF;
401
402 return 0;
403}
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
433 int len, int check)
434{
435 struct ubi_volume *vol = desc->vol;
436 struct ubi_device *ubi = vol->ubi;
437 int err, vol_id = vol->vol_id;
438
439 dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
440
441 err = leb_read_sanity_check(desc, lnum, offset, len);
442 if (err < 0)
443 return err;
444
445 if (len == 0)
446 return 0;
447
448 err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
449 if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
450 ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
451 vol->corrupted = 1;
452 }
453
454 return err;
455}
456EXPORT_SYMBOL_GPL(ubi_leb_read);
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472int ubi_leb_read_sg(struct ubi_volume_desc *desc, int lnum, struct ubi_sgl *sgl,
473 int offset, int len, int check)
474{
475 struct ubi_volume *vol = desc->vol;
476 struct ubi_device *ubi = vol->ubi;
477 int err, vol_id = vol->vol_id;
478
479 dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
480
481 err = leb_read_sanity_check(desc, lnum, offset, len);
482 if (err < 0)
483 return err;
484
485 if (len == 0)
486 return 0;
487
488 err = ubi_eba_read_leb_sg(ubi, vol, sgl, lnum, offset, len, check);
489 if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
490 ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
491 vol->corrupted = 1;
492 }
493
494 return err;
495}
496EXPORT_SYMBOL_GPL(ubi_leb_read_sg);
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
524 int offset, int len)
525{
526 struct ubi_volume *vol = desc->vol;
527 struct ubi_device *ubi = vol->ubi;
528 int vol_id = vol->vol_id;
529
530 dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
531
532 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
533 return -EINVAL;
534
535 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
536 return -EROFS;
537
538 if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
539 offset + len > vol->usable_leb_size ||
540 offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
541 return -EINVAL;
542
543 if (vol->upd_marker)
544 return -EBADF;
545
546 if (len == 0)
547 return 0;
548
549 return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len);
550}
551EXPORT_SYMBOL_GPL(ubi_leb_write);
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
569 int len)
570{
571 struct ubi_volume *vol = desc->vol;
572 struct ubi_device *ubi = vol->ubi;
573 int vol_id = vol->vol_id;
574
575 dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
576
577 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
578 return -EINVAL;
579
580 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
581 return -EROFS;
582
583 if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
584 len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
585 return -EINVAL;
586
587 if (vol->upd_marker)
588 return -EBADF;
589
590 if (len == 0)
591 return 0;
592
593 return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len);
594}
595EXPORT_SYMBOL_GPL(ubi_leb_change);
596
597
598
599
600
601
602
603
604
605
606
607
608
609int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
610{
611 struct ubi_volume *vol = desc->vol;
612 struct ubi_device *ubi = vol->ubi;
613 int err;
614
615 dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
616
617 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
618 return -EROFS;
619
620 if (lnum < 0 || lnum >= vol->reserved_pebs)
621 return -EINVAL;
622
623 if (vol->upd_marker)
624 return -EBADF;
625
626 err = ubi_eba_unmap_leb(ubi, vol, lnum);
627 if (err)
628 return err;
629
630 return ubi_wl_flush(ubi, vol->vol_id, lnum);
631}
632EXPORT_SYMBOL_GPL(ubi_leb_erase);
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
671{
672 struct ubi_volume *vol = desc->vol;
673 struct ubi_device *ubi = vol->ubi;
674
675 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
676
677 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
678 return -EROFS;
679
680 if (lnum < 0 || lnum >= vol->reserved_pebs)
681 return -EINVAL;
682
683 if (vol->upd_marker)
684 return -EBADF;
685
686 return ubi_eba_unmap_leb(ubi, vol, lnum);
687}
688EXPORT_SYMBOL_GPL(ubi_leb_unmap);
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706int ubi_leb_map(struct ubi_volume_desc *desc, int lnum)
707{
708 struct ubi_volume *vol = desc->vol;
709 struct ubi_device *ubi = vol->ubi;
710
711 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
712
713 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
714 return -EROFS;
715
716 if (lnum < 0 || lnum >= vol->reserved_pebs)
717 return -EINVAL;
718
719 if (vol->upd_marker)
720 return -EBADF;
721
722 if (vol->eba_tbl[lnum] >= 0)
723 return -EBADMSG;
724
725 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
726}
727EXPORT_SYMBOL_GPL(ubi_leb_map);
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
746{
747 struct ubi_volume *vol = desc->vol;
748
749 dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
750
751 if (lnum < 0 || lnum >= vol->reserved_pebs)
752 return -EINVAL;
753
754 if (vol->upd_marker)
755 return -EBADF;
756
757 return vol->eba_tbl[lnum] >= 0;
758}
759EXPORT_SYMBOL_GPL(ubi_is_mapped);
760
761
762
763
764
765
766
767
768
769int ubi_sync(int ubi_num)
770{
771 struct ubi_device *ubi;
772
773 ubi = ubi_get_device(ubi_num);
774 if (!ubi)
775 return -ENODEV;
776
777 mtd_sync(ubi->mtd);
778 ubi_put_device(ubi);
779 return 0;
780}
781EXPORT_SYMBOL_GPL(ubi_sync);
782
783
784
785
786
787
788
789
790
791
792
793
794
795int ubi_flush(int ubi_num, int vol_id, int lnum)
796{
797 struct ubi_device *ubi;
798 int err = 0;
799
800 ubi = ubi_get_device(ubi_num);
801 if (!ubi)
802 return -ENODEV;
803
804 err = ubi_wl_flush(ubi, vol_id, lnum);
805 ubi_put_device(ubi);
806 return err;
807}
808EXPORT_SYMBOL_GPL(ubi_flush);
809
810BLOCKING_NOTIFIER_HEAD(ubi_notifiers);
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828int ubi_register_volume_notifier(struct notifier_block *nb,
829 int ignore_existing)
830{
831 int err;
832
833 err = blocking_notifier_chain_register(&ubi_notifiers, nb);
834 if (err != 0)
835 return err;
836 if (ignore_existing)
837 return 0;
838
839
840
841
842
843
844
845 mutex_lock(&ubi_devices_mutex);
846 ubi_enumerate_volumes(nb);
847 mutex_unlock(&ubi_devices_mutex);
848
849 return err;
850}
851EXPORT_SYMBOL_GPL(ubi_register_volume_notifier);
852
853
854
855
856
857
858
859
860int ubi_unregister_volume_notifier(struct notifier_block *nb)
861{
862 return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
863}
864EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);
865