add missing includes
[project/fstools.git] / libubi / libubi.c
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., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  * Author: Artem Bityutskiy
19  *
20  * UBI (Unsorted Block Images) library.
21  */
22
23 #define PROGRAM_NAME "libubi"
24
25 #include <sys/sysmacros.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <fcntl.h>
30 #include <dirent.h>
31 #include <unistd.h>
32 #include <limits.h>
33 #include <sys/ioctl.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include "libubi.h"
37 #include "libubi_int.h"
38
39 /**
40  * mkpath - compose full path from 2 given components.
41  * @path: the first component
42  * @name: the second component
43  *
44  * This function returns the resulting path in case of success and %NULL in
45  * case of failure.
46  */
47 static char *mkpath(const char *path, const char *name)
48 {
49         char *n;
50         int len1 = strlen(path);
51         int len2 = strlen(name);
52
53         n = malloc(len1 + len2 + 2);
54         if (!n) {
55                 sys_errmsg("cannot allocate %d bytes", len1 + len2 + 2);
56                 return NULL;
57         }
58
59         memcpy(n, path, len1);
60         if (n[len1 - 1] != '/')
61                 n[len1++] = '/';
62
63         memcpy(n + len1, name, len2 + 1);
64         return n;
65 }
66
67 /**
68  * read_positive_ll - read a positive 'long long' value from a file.
69  * @file: the file to read from
70  * @value: the result is stored here
71  *
72  * This function reads file @file and interprets its contents as a positive
73  * 'long long' integer. If this is not true, it fails with %EINVAL error code.
74  * Returns %0 in case of success and %-1 in case of failure.
75  */
76 static int read_positive_ll(const char *file, long long *value)
77 {
78         int fd, rd;
79         char buf[50];
80
81         fd = open(file, O_RDONLY);
82         if (fd == -1)
83                 return -1;
84
85         rd = read(fd, buf, sizeof(buf));
86         if (rd == -1) {
87                 sys_errmsg("cannot read \"%s\"", file);
88                 goto out_error;
89         }
90         if (rd == sizeof(buf)) {
91                 errmsg("contents of \"%s\" is too long", file);
92                 errno = EINVAL;
93                 goto out_error;
94         }
95         buf[rd] = '\0';
96
97         if (sscanf(buf, "%lld\n", value) != 1) {
98                 errmsg("cannot read integer from \"%s\"\n", file);
99                 errno = EINVAL;
100                 goto out_error;
101         }
102
103         if (*value < 0) {
104                 errmsg("negative value %lld in \"%s\"", *value, file);
105                 errno = EINVAL;
106                 goto out_error;
107         }
108
109         if (close(fd)) {
110                 sys_errmsg("close failed on \"%s\"", file);
111                 return -1;
112         }
113
114         return 0;
115
116 out_error:
117         close(fd);
118         return -1;
119 }
120
121 /**
122  * read_positive_int - read a positive 'int' value from a file.
123  * @file: the file to read from
124  * @value: the result is stored here
125  *
126  * This function is the same as 'read_positive_ll()', but it reads an 'int'
127  * value, not 'long long'.
128  */
129 static int read_positive_int(const char *file, int *value)
130 {
131         long long res;
132
133         if (read_positive_ll(file, &res))
134                 return -1;
135
136         /* Make sure the value is not too big */
137         if (res > INT_MAX) {
138                 errmsg("value %lld read from file \"%s\" is out of range",
139                        res, file);
140                 errno = EINVAL;
141                 return -1;
142         }
143
144         *value = res;
145         return 0;
146 }
147
148 /**
149  * read_data - read data from a file.
150  * @file: the file to read from
151  * @buf: the buffer to read to
152  * @buf_len: buffer length
153  *
154  * This function returns number of read bytes in case of success and %-1 in
155  * case of failure. Note, if the file contains more then @buf_len bytes of
156  * date, this function fails with %EINVAL error code.
157  */
158 static int read_data(const char *file, void *buf, int buf_len)
159 {
160         int fd, rd, tmp, tmp1;
161
162         fd = open(file, O_RDONLY);
163         if (fd == -1)
164                 return -1;
165
166         rd = read(fd, buf, buf_len);
167         if (rd == -1) {
168                 sys_errmsg("cannot read \"%s\"", file);
169                 goto out_error;
170         }
171
172         if (rd == buf_len) {
173                 errmsg("contents of \"%s\" is too long", file);
174                 errno = EINVAL;
175                 goto out_error;
176         }
177
178         ((char *)buf)[rd] = '\0';
179
180         /* Make sure all data is read */
181         tmp1 = read(fd, &tmp, 1);
182         if (tmp1 == 1) {
183                 sys_errmsg("cannot read \"%s\"", file);
184                 goto out_error;
185         }
186         if (tmp1) {
187                 errmsg("file \"%s\" contains too much data (> %d bytes)",
188                        file, buf_len);
189                 errno = EINVAL;
190                 goto out_error;
191         }
192
193         if (close(fd)) {
194                 sys_errmsg("close failed on \"%s\"", file);
195                 return -1;
196         }
197
198         return rd;
199
200 out_error:
201         close(fd);
202         return -1;
203 }
204
205 /**
206  * read_major - read major and minor numbers from a file.
207  * @file: name of the file to read from
208  * @major: major number is returned here
209  * @minor: minor number is returned here
210  *
211  * This function returns % in case of succes, and %-1 in case of failure.
212  */
213 static int read_major(const char *file, int *major, int *minor)
214 {
215         int ret;
216         char buf[50];
217
218         ret = read_data(file, buf, 50);
219         if (ret < 0)
220                 return ret;
221
222         ret = sscanf(buf, "%d:%d\n", major, minor);
223         if (ret != 2) {
224                 errno = EINVAL;
225                 errmsg("\"%s\" does not have major:minor format", file);
226                 return -1;
227         }
228
229         if (*major < 0 || *minor < 0) {
230                 errno = EINVAL;
231                 errmsg("bad major:minor %d:%d in \"%s\"",
232                               *major, *minor, file);
233                 return -1;
234         }
235
236         return 0;
237 }
238
239 /**
240  * dev_read_int - read a positive 'int' value from an UBI device sysfs file.
241  * @patt: file pattern to read from
242  * @dev_num: UBI device number
243  * @value: the result is stored here
244  *
245  * This function returns %0 in case of success and %-1 in case of failure.
246  */
247 static int dev_read_int(const char *patt, int dev_num, int *value)
248 {
249         char file[strlen(patt) + 50];
250
251         sprintf(file, patt, dev_num);
252         return read_positive_int(file, value);
253 }
254
255 /**
256  * vol_read_int - read a positive 'int' value from an UBI volume sysfs file.
257  * @patt: file pattern to read from
258  * @dev_num: UBI device number
259  * @vol_id: volume ID
260  * @value: the result is stored here
261  *
262  * This function returns %0 in case of success and %-1 in case of failure.
263  */
264 static int vol_read_int(const char *patt, int dev_num, int vol_id, int *value)
265 {
266         char file[strlen(patt) + 100];
267
268         sprintf(file, patt, dev_num, vol_id);
269         return read_positive_int(file, value);
270 }
271
272 /**
273  * dev_read_ll - read a positive 'long long' value from an UBI device sysfs file.
274  * @patt: file pattern to read from
275  * @dev_num: UBI device number
276  * @value: the result is stored here
277  *
278  * This function returns %0 in case of success and %-1 in case of failure.
279  */
280 static int dev_read_ll(const char *patt, int dev_num, long long *value)
281 {
282         char file[strlen(patt) + 50];
283
284         sprintf(file, patt, dev_num);
285         return read_positive_ll(file, value);
286 }
287
288 /**
289  * vol_read_ll - read a positive 'long long' value from an UBI volume sysfs file.
290  * @patt: file pattern to read from
291  * @dev_num: UBI device number
292  * @vol_id: volume ID
293  * @value: the result is stored here
294  *
295  * This function returns %0 in case of success and %-1 in case of failure.
296  */
297 static int vol_read_ll(const char *patt, int dev_num, int vol_id,
298                        long long *value)
299 {
300         char file[strlen(patt) + 100];
301
302         sprintf(file, patt, dev_num, vol_id);
303         return read_positive_ll(file, value);
304 }
305
306 /**
307  * vol_read_data - read data from an UBI volume's sysfs file.
308  * @patt: file pattern to read from
309  * @dev_num: UBI device number
310  * @vol_id: volume ID
311  * @buf: buffer to read to
312  * @buf_len: buffer length
313  *
314  * This function returns number of read bytes in case of success and %-1 in
315  * case of failure.
316  */
317 static int vol_read_data(const char *patt, int dev_num, int vol_id, void *buf,
318                          int buf_len)
319 {
320         char file[strlen(patt) + 100];
321
322         sprintf(file, patt, dev_num, vol_id);
323         return read_data(file, buf, buf_len);
324 }
325
326 /**
327  * dev_get_major - get major and minor numbers of an UBI device.
328  * @lib: libubi descriptor
329  * @dev_num: UBI device number
330  * @major: major number is returned here
331  * @minor: minor number is returned here
332  *
333  * This function returns zero in case of succes and %-1 in case of failure.
334  */
335 static int dev_get_major(struct libubi *lib, int dev_num, int *major, int *minor)
336 {
337         char file[strlen(lib->dev_dev) + 50];
338
339         sprintf(file, lib->dev_dev, dev_num);
340         return read_major(file, major, minor);
341 }
342
343 /**
344  * vol_get_major - get major and minor numbers of an UBI volume.
345  * @lib: libubi descriptor
346  * @dev_num: UBI device number
347  * @vol_id: volume ID
348  * @major: major number is returned here
349  * @minor: minor number is returned here
350  *
351  * This function returns zero in case of succes and %-1 in case of failure.
352  */
353 static int vol_get_major(struct libubi *lib, int dev_num, int vol_id,
354                          int *major, int *minor)
355 {
356         char file[strlen(lib->vol_dev) + 100];
357
358         sprintf(file, lib->vol_dev, dev_num, vol_id);
359         return read_major(file, major, minor);
360 }
361
362 /**
363  * vol_node2nums - find UBI device number and volume ID by volume device node
364  *                 file.
365  * @lib: UBI library descriptor
366  * @node: UBI character device node name
367  * @dev_num: UBI device number is returned here
368  * @vol_id: volume ID is returned hers
369  *
370  * This function returns zero in case of succes and %-1 in case of failure.
371  */
372 static int vol_node2nums(struct libubi *lib, const char *node, int *dev_num,
373                          int *vol_id)
374 {
375         struct stat st;
376         struct ubi_info info;
377         int i, fd, major, minor;
378         char file[strlen(lib->ubi_vol) + 100];
379
380         if (stat(node, &st)) {
381                 sys_errmsg("cannot get information about \"%s\"",
382                                   node);
383                 return -1;
384         }
385         if (!S_ISCHR(st.st_mode)) {
386                 errno = EINVAL;
387                 errmsg("\"%s\" is not a character device", node);
388                 return -1;
389         }
390
391         major = major(st.st_rdev);
392         minor = minor(st.st_rdev);
393
394         if (minor == 0) {
395                 errno = EINVAL;
396                 errmsg("\"%s\" is not a volume character device", node);
397                 return -1;
398         }
399
400         if (ubi_get_info((libubi_t *)lib, &info))
401                 return -1;
402
403         for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
404                 int major1, minor1, ret;
405
406                 ret = dev_get_major(lib, i, &major1, &minor1);
407                 if (ret) {
408                         if (errno == ENOENT)
409                                 continue;
410                         return -1;
411                 }
412
413                 if (major1 == major)
414                         break;
415         }
416
417         if (i > info.highest_dev_num) {
418                 errno = ENODEV;
419                 return -1;
420         }
421
422         /* Make sure this UBI volume exists */
423         sprintf(file, lib->ubi_vol, i, minor - 1);
424         fd = open(file, O_RDONLY);
425         if (fd == -1) {
426                 errno = ENODEV;
427                 return -1;
428         }
429
430         *dev_num = i;
431         *vol_id = minor - 1;
432         errno = 0;
433         return 0;
434 }
435
436 /**
437  * dev_node2num - find UBI device number by its character device node.
438  * @lib: UBI library descriptor
439  * @node: UBI character device node name
440  * @dev_num: UBI device number is returned here
441  *
442  * This function returns %0 in case of success and %-1 in case of failure.
443  */
444 static int dev_node2num(struct libubi *lib, const char *node, int *dev_num)
445 {
446         struct stat st;
447         struct ubi_info info;
448         int i, major, minor;
449
450         if (stat(node, &st)) {
451                 sys_errmsg("cannot get information about \"%s\"", node);
452                 return -1;
453         }
454         if (!S_ISCHR(st.st_mode)) {
455                 errno = EINVAL;
456                 errmsg("\"%s\" is not a character device", node);
457                 return -1;
458         }
459
460         major = major(st.st_rdev);
461         minor = minor(st.st_rdev);
462
463         if (minor != 0) {
464                 errno = EINVAL;
465                 errmsg("\"%s\" is not an UBI character device", node);
466                 return -1;
467         }
468
469         if (ubi_get_info((libubi_t *)lib, &info))
470                 return -1;
471
472         for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
473                 int major1, minor1, ret;
474
475                 ret = dev_get_major(lib, i, &major1, &minor1);
476                 if (ret) {
477                         if (errno == ENOENT)
478                                 continue;
479                         return -1;
480                 }
481
482                 if (major1 == major) {
483                         if (minor1 != 0) {
484                                 errmsg("UBI character device minor number is "
485                                        "%d, but must be 0", minor1);
486                                 errno = EINVAL;
487                                 return -1;
488                         }
489                         errno = 0;
490                         *dev_num = i;
491                         return 0;
492                 }
493         }
494
495         errno = ENODEV;
496         return -1;
497 }
498
499 int mtd_num2ubi_dev(libubi_t desc, int mtd_num, int *dev_num)
500 {
501         struct ubi_info info;
502         int i, ret, mtd_num1;
503         struct libubi *lib = desc;
504
505         if (ubi_get_info(desc, &info))
506                 return -1;
507
508         for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
509                 ret = dev_read_int(lib->dev_mtd_num, i, &mtd_num1);
510                 if (ret) {
511                         if (errno == ENOENT)
512                                 continue;
513                         return -1;
514                 }
515
516                 if (mtd_num1 == mtd_num) {
517                         errno = 0;
518                         *dev_num = i;
519                         return 0;
520                 }
521         }
522
523         errno = 0;
524         return -1;
525 }
526
527 libubi_t libubi_open(void)
528 {
529         int fd, version;
530         struct libubi *lib;
531
532         lib = calloc(1, sizeof(struct libubi));
533         if (!lib)
534                 return NULL;
535
536         lib->sysfs_ctrl = mkpath("/sys", SYSFS_CTRL);
537         if (!lib->sysfs_ctrl)
538                 goto out_error;
539
540         lib->ctrl_dev = mkpath(lib->sysfs_ctrl, CTRL_DEV);
541         if (!lib->ctrl_dev)
542                 goto out_error;
543
544         lib->sysfs_ubi = mkpath("/sys", SYSFS_UBI);
545         if (!lib->sysfs_ubi)
546                 goto out_error;
547
548         /* Make sure UBI is present */
549         fd = open(lib->sysfs_ubi, O_RDONLY);
550         if (fd == -1) {
551                 errno = 0;
552                 goto out_error;
553         }
554
555         if (close(fd)) {
556                 sys_errmsg("close failed on \"%s\"", lib->sysfs_ubi);
557                 goto out_error;
558         }
559
560         lib->ubi_dev = mkpath(lib->sysfs_ubi, UBI_DEV_NAME_PATT);
561         if (!lib->ubi_dev)
562                 goto out_error;
563
564         lib->ubi_version = mkpath(lib->sysfs_ubi, UBI_VER);
565         if (!lib->ubi_version)
566                 goto out_error;
567
568         lib->dev_dev = mkpath(lib->ubi_dev, DEV_DEV);
569         if (!lib->dev_dev)
570                 goto out_error;
571
572         lib->dev_avail_ebs = mkpath(lib->ubi_dev, DEV_AVAIL_EBS);
573         if (!lib->dev_avail_ebs)
574                 goto out_error;
575
576         lib->dev_total_ebs = mkpath(lib->ubi_dev, DEV_TOTAL_EBS);
577         if (!lib->dev_total_ebs)
578                 goto out_error;
579
580         lib->dev_bad_count = mkpath(lib->ubi_dev, DEV_BAD_COUNT);
581         if (!lib->dev_bad_count)
582                 goto out_error;
583
584         lib->dev_eb_size = mkpath(lib->ubi_dev, DEV_EB_SIZE);
585         if (!lib->dev_eb_size)
586                 goto out_error;
587
588         lib->dev_max_ec = mkpath(lib->ubi_dev, DEV_MAX_EC);
589         if (!lib->dev_max_ec)
590                 goto out_error;
591
592         lib->dev_bad_rsvd = mkpath(lib->ubi_dev, DEV_MAX_RSVD);
593         if (!lib->dev_bad_rsvd)
594                 goto out_error;
595
596         lib->dev_max_vols = mkpath(lib->ubi_dev, DEV_MAX_VOLS);
597         if (!lib->dev_max_vols)
598                 goto out_error;
599
600         lib->dev_min_io_size = mkpath(lib->ubi_dev, DEV_MIN_IO_SIZE);
601         if (!lib->dev_min_io_size)
602                 goto out_error;
603
604         lib->dev_mtd_num = mkpath(lib->ubi_dev, DEV_MTD_NUM);
605         if (!lib->dev_mtd_num)
606                 goto out_error;
607
608         lib->ubi_vol = mkpath(lib->sysfs_ubi, UBI_VOL_NAME_PATT);
609         if (!lib->ubi_vol)
610                 goto out_error;
611
612         lib->vol_type = mkpath(lib->ubi_vol, VOL_TYPE);
613         if (!lib->vol_type)
614                 goto out_error;
615
616         lib->vol_dev = mkpath(lib->ubi_vol, VOL_DEV);
617         if (!lib->vol_dev)
618                 goto out_error;
619
620         lib->vol_alignment = mkpath(lib->ubi_vol, VOL_ALIGNMENT);
621         if (!lib->vol_alignment)
622                 goto out_error;
623
624         lib->vol_data_bytes = mkpath(lib->ubi_vol, VOL_DATA_BYTES);
625         if (!lib->vol_data_bytes)
626                 goto out_error;
627
628         lib->vol_rsvd_ebs = mkpath(lib->ubi_vol, VOL_RSVD_EBS);
629         if (!lib->vol_rsvd_ebs)
630                 goto out_error;
631
632         lib->vol_eb_size = mkpath(lib->ubi_vol, VOL_EB_SIZE);
633         if (!lib->vol_eb_size)
634                 goto out_error;
635
636         lib->vol_corrupted = mkpath(lib->ubi_vol, VOL_CORRUPTED);
637         if (!lib->vol_corrupted)
638                 goto out_error;
639
640         lib->vol_name = mkpath(lib->ubi_vol, VOL_NAME);
641         if (!lib->vol_name)
642                 goto out_error;
643
644         if (read_positive_int(lib->ubi_version, &version))
645                 goto out_error;
646         if (version != LIBUBI_UBI_VERSION) {
647                 errmsg("this library was made for UBI version %d, but UBI "
648                        "version %d is detected\n", LIBUBI_UBI_VERSION, version);
649                 goto out_error;
650         }
651
652         return lib;
653
654 out_error:
655         libubi_close((libubi_t)lib);
656         return NULL;
657 }
658
659 void libubi_close(libubi_t desc)
660 {
661         struct libubi *lib = (struct libubi *)desc;
662
663         free(lib->vol_name);
664         free(lib->vol_corrupted);
665         free(lib->vol_eb_size);
666         free(lib->vol_rsvd_ebs);
667         free(lib->vol_data_bytes);
668         free(lib->vol_alignment);
669         free(lib->vol_dev);
670         free(lib->vol_type);
671         free(lib->ubi_vol);
672         free(lib->dev_mtd_num);
673         free(lib->dev_min_io_size);
674         free(lib->dev_max_vols);
675         free(lib->dev_bad_rsvd);
676         free(lib->dev_max_ec);
677         free(lib->dev_eb_size);
678         free(lib->dev_bad_count);
679         free(lib->dev_total_ebs);
680         free(lib->dev_avail_ebs);
681         free(lib->dev_dev);
682         free(lib->ubi_version);
683         free(lib->ubi_dev);
684         free(lib->sysfs_ubi);
685         free(lib->ctrl_dev);
686         free(lib->sysfs_ctrl);
687         free(lib);
688 }
689
690 /**
691  * do_attach - perform the actual attach operation.
692  * @node: name of the UBI control character device node
693  * @r: attach request
694  *
695  * This function performs the actual UBI attach operation. Returns %0 in case of
696  * success and %-1 in case of failure. @r->ubi_num contains newly created UBI
697  * device number.
698  */
699 static int do_attach(const char *node, const struct ubi_attach_req *r)
700 {
701         int fd, ret;
702
703         fd = open(node, O_RDONLY);
704         if (fd == -1) {
705                 sys_errmsg("cannot open \"%s\"", node);
706                 return -1;
707         }
708         ret = ioctl(fd, UBI_IOCATT, r);
709         close(fd);
710         if (ret == -1)
711                 return -1;
712
713 #ifdef UDEV_SETTLE_HACK
714 //      if (system("udevsettle") == -1)
715 //              return -1;
716         usleep(100000);
717 #endif
718         return ret;
719 }
720
721 #ifndef MTD_CHAR_MAJOR
722 /*
723  * This is taken from kernel <linux/mtd/mtd.h> and is unlikely to change anytime
724  * soon.
725  */
726 #define MTD_CHAR_MAJOR 90
727 #endif
728
729 /**
730  * mtd_node_to_num - converts device node to MTD number.
731  * @mtd_dev_node: path to device node to convert
732  *
733  * This function converts given @mtd_dev_node to MTD device number.
734  * @mtd_dev_node should contain path to the MTD device node. Returns MTD device
735  * number in case of success and %-1 in case of failure (errno is set).
736  */
737 static int mtd_node_to_num(const char *mtd_dev_node)
738 {
739         int major, minor;
740         struct stat sb;
741
742         if (stat(mtd_dev_node, &sb) < 0) {
743                 sys_errmsg("cannot stat \"%s\"", mtd_dev_node);
744                 return -1;
745         }
746
747         if (!S_ISCHR(sb.st_mode)) {
748                 errno = EINVAL;
749                 sys_errmsg("\"%s\" is not a character device",
750                                   mtd_dev_node);
751                 return -1;
752         }
753
754         major = major(sb.st_rdev);
755         minor = minor(sb.st_rdev);
756
757         if (major != MTD_CHAR_MAJOR) {
758                 errno = EINVAL;
759                 sys_errmsg("\"%s\" is not an MTD device", mtd_dev_node);
760                 return -1;
761         }
762
763         return minor / 2;
764 }
765
766 int ubi_attach(libubi_t desc, const char *node, struct ubi_attach_request *req)
767 {
768         struct ubi_attach_req r;
769         int ret;
770
771         (void)desc;
772
773         if (req->mtd_dev_node) {
774                 /*
775                  * User has passed path to device node. Lets find out MTD
776                  * device number of the device and update req->mtd_num with it
777                  */
778                 req->mtd_num = mtd_node_to_num(req->mtd_dev_node);
779                 if (req->mtd_num == -1)
780                         return -1;
781         }
782
783         memset(&r, 0, sizeof(struct ubi_attach_req));
784         r.ubi_num = req->dev_num;
785         r.mtd_num = req->mtd_num;
786         r.vid_hdr_offset = req->vid_hdr_offset;
787
788         if (req->max_beb_per1024) {
789                 /*
790                  * We first have to check if the running kernel supports the
791                  * 'max_beb_per1024' parameter. To do this, we invoke the
792                  * "attach" ioctl 2 times: first with incorrect value %-1 of
793                  * 'max_beb_per1024'.
794                  *
795                  * If the ioctl succeeds, it means that the kernel doesn't
796                  * support the feature and just ignored our 'max_beb_per1024'
797                  * value.
798                  *
799                  * If the ioctl returns -EINVAL, we assume this is because
800                  * 'max_beb_per1024' was set to -1, and we invoke the ioctl for
801                  * the second time with the 'max_beb_per1024' value.
802                  */
803                 r.max_beb_per1024 = -1;
804                 ret = do_attach(node, &r);
805                 if (ret == 0) {
806                         req->dev_num = r.ubi_num;
807                         /*
808                          * The call succeeded. It means that the kernel ignored
809                          * 'max_beb_per1024' parameter. 
810                          */
811                         return 1;
812                 } else if (errno != EINVAL)
813                         return ret;
814         }
815
816         r.max_beb_per1024 = req->max_beb_per1024;
817
818         ret = do_attach(node, &r);
819         if (ret == 0)
820                 req->dev_num = r.ubi_num;
821
822         return ret;
823 }
824
825 int ubi_detach_mtd(libubi_t desc, const char *node, int mtd_num)
826 {
827         int ret, ubi_dev;
828
829         ret = mtd_num2ubi_dev(desc, mtd_num, &ubi_dev);
830         if (ret == -1) {
831                 errno = ENODEV;
832                 return ret;
833         }
834
835         return ubi_remove_dev(desc, node, ubi_dev);
836 }
837
838 int ubi_detach(libubi_t desc, const char *node, const char *mtd_dev_node)
839 {
840         int mtd_num;
841
842         if (!mtd_dev_node) {
843                 errno = EINVAL;
844                 return -1;
845         }
846
847         mtd_num = mtd_node_to_num(mtd_dev_node);
848         if (mtd_num == -1)
849                 return -1;
850
851         return ubi_detach_mtd(desc, node, mtd_num);
852 }
853
854 int ubi_remove_dev(libubi_t desc, const char *node, int ubi_dev)
855 {
856         int fd, ret;
857
858         desc = desc;
859
860         fd = open(node, O_RDONLY);
861         if (fd == -1) {
862                 sys_errmsg("cannot open \"%s\"", node);
863                 return -1;
864         }
865         ret = ioctl(fd, UBI_IOCFDET, &ubi_dev);
866         if (ret == -1)
867                 goto out_close;
868
869 #ifdef UDEV_SETTLE_HACK
870 //      if (system("udevsettle") == -1)
871 //              return -1;
872         usleep(100000);
873 #endif
874
875 out_close:
876         close(fd);
877         return ret;
878 }
879
880 int ubi_probe_node(libubi_t desc, const char *node)
881 {
882         struct stat st;
883         struct ubi_info info;
884         int i, fd, major, minor;
885         struct libubi *lib = (struct libubi *)desc;
886         char file[strlen(lib->ubi_vol) + 100];
887
888         if (stat(node, &st)) {
889                 sys_errmsg("cannot get information about \"%s\"", node);
890                 return -1;
891         }
892
893         if (!S_ISCHR(st.st_mode)) {
894                 errmsg("\"%s\" is not a character device", node);
895                 errno = EINVAL;
896                 return -1;
897         }
898
899         major = major(st.st_rdev);
900         minor = minor(st.st_rdev);
901
902         if (ubi_get_info((libubi_t *)lib, &info))
903                 return -1;
904
905         for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
906                 int major1, minor1, ret;
907
908                 ret = dev_get_major(lib, i, &major1, &minor1);
909                 if (ret) {
910                         if (errno == ENOENT)
911                                 continue;
912                         if (!errno)
913                                 goto out_not_ubi;
914                         return -1;
915                 }
916
917                 if (major1 == major)
918                         break;
919         }
920
921         if (i > info.highest_dev_num)
922                 goto out_not_ubi;
923
924         if (minor == 0)
925                 return 1;
926
927         /* This is supposdely an UBI volume device node */
928         sprintf(file, lib->ubi_vol, i, minor - 1);
929         fd = open(file, O_RDONLY);
930         if (fd == -1)
931                 goto out_not_ubi;
932
933         return 2;
934
935 out_not_ubi:
936         errmsg("\"%s\" has major:minor %d:%d, but this does not correspond to "
937                "any existing UBI device or volume", node, major, minor);
938         errno = ENODEV;
939         return -1;
940 }
941
942 int ubi_get_info(libubi_t desc, struct ubi_info *info)
943 {
944         DIR *sysfs_ubi;
945         struct dirent *dirent;
946         struct libubi *lib = (struct libubi *)desc;
947
948         memset(info, 0, sizeof(struct ubi_info));
949
950         if (read_major(lib->ctrl_dev, &info->ctrl_major, &info->ctrl_minor)) {
951                 /*
952                  * Older UBI versions did not have control device, so we do not
953                  * panic here for compatibility reasons. May be few years later
954                  * we could return -1 here, but for now just set major:minor to
955                  * -1.
956                  */
957                 info->ctrl_major = info->ctrl_minor = -1;
958         }
959
960         /*
961          * We have to scan the UBI sysfs directory to identify how many UBI
962          * devices are present.
963          */
964         sysfs_ubi = opendir(lib->sysfs_ubi);
965         if (!sysfs_ubi)
966                 return -1;
967
968         info->lowest_dev_num = INT_MAX;
969         while (1) {
970                 int dev_num, ret;
971                 char tmp_buf[256];
972
973                 errno = 0;
974                 dirent = readdir(sysfs_ubi);
975                 if (!dirent)
976                         break;
977
978                 if (strlen(dirent->d_name) >= 255) {
979                         errmsg("invalid entry in %s: \"%s\"",
980                                lib->sysfs_ubi, dirent->d_name);
981                         errno = EINVAL;
982                         goto out_close;
983                 }
984
985                 ret = sscanf(dirent->d_name, UBI_DEV_NAME_PATT"%s",
986                              &dev_num, tmp_buf);
987                 if (ret == 1) {
988                         info->dev_count += 1;
989                         if (dev_num > info->highest_dev_num)
990                                 info->highest_dev_num = dev_num;
991                         if (dev_num < info->lowest_dev_num)
992                                 info->lowest_dev_num = dev_num;
993                 }
994         }
995
996         if (!dirent && errno) {
997                 sys_errmsg("readdir failed on \"%s\"", lib->sysfs_ubi);
998                 goto out_close;
999         }
1000
1001         if (closedir(sysfs_ubi)) {
1002                 sys_errmsg("closedir failed on \"%s\"", lib->sysfs_ubi);
1003                 return -1;
1004         }
1005         if (info->lowest_dev_num == INT_MAX)
1006                 info->lowest_dev_num = 0;
1007
1008         if (read_positive_int(lib->ubi_version, &info->version))
1009                 return -1;
1010
1011         return 0;
1012
1013 out_close:
1014         closedir(sysfs_ubi);
1015         return -1;
1016 }
1017
1018 int ubi_mkvol(libubi_t desc, const char *node, struct ubi_mkvol_request *req)
1019 {
1020         int fd, ret;
1021         struct ubi_mkvol_req r;
1022         size_t n;
1023
1024         memset(&r, 0, sizeof(struct ubi_mkvol_req));
1025
1026         desc = desc;
1027         r.vol_id = req->vol_id;
1028         r.alignment = req->alignment;
1029         r.bytes = req->bytes;
1030         r.vol_type = req->vol_type;
1031
1032         n = strlen(req->name);
1033         if (n > UBI_MAX_VOLUME_NAME)
1034                 return -1;
1035
1036         strncpy(r.name, req->name, UBI_MAX_VOLUME_NAME + 1);
1037         r.name_len = n;
1038
1039         desc = desc;
1040         fd = open(node, O_RDONLY);
1041         if (fd == -1) {
1042                 sys_errmsg("cannot open \"%s\"", node);
1043                 return -1;
1044         }
1045         ret = ioctl(fd, UBI_IOCMKVOL, &r);
1046         if (ret == -1) {
1047                 close(fd);
1048                 return ret;
1049         }
1050
1051         close(fd);
1052         req->vol_id = r.vol_id;
1053
1054 #ifdef UDEV_SETTLE_HACK
1055 //      if (system("udevsettle") == -1)
1056 //              return -1;
1057         usleep(100000);
1058 #endif
1059
1060         return 0;
1061 }
1062
1063 int ubi_rmvol(libubi_t desc, const char *node, int vol_id)
1064 {
1065         int fd, ret;
1066
1067         desc = desc;
1068         fd = open(node, O_RDONLY);
1069         if (fd == -1) {
1070                 sys_errmsg("cannot open \"%s\"", node);
1071                 return -1;
1072         }
1073
1074         ret = ioctl(fd, UBI_IOCRMVOL, &vol_id);
1075         if (ret == -1) {
1076                 close(fd);
1077                 return ret;
1078         }
1079
1080         close(fd);
1081
1082 #ifdef UDEV_SETTLE_HACK
1083 //      if (system("udevsettle") == -1)
1084 //              return -1;
1085         usleep(100000);
1086 #endif
1087
1088         return 0;
1089 }
1090
1091 int ubi_rnvols(libubi_t desc, const char *node, struct ubi_rnvol_req *rnvol)
1092 {
1093         int fd, ret;
1094
1095         desc = desc;
1096         fd = open(node, O_RDONLY);
1097         if (fd == -1)
1098                 return -1;
1099
1100         ret = ioctl(fd, UBI_IOCRNVOL, rnvol);
1101         if (ret == -1) {
1102                 close(fd);
1103                 return ret;
1104         }
1105
1106         close(fd);
1107
1108 #ifdef UDEV_SETTLE_HACK
1109 //      if (system("udevsettle") == -1)
1110 //              return -1;
1111         usleep(100000);
1112 #endif
1113
1114         return 0;
1115 }
1116
1117 int ubi_rsvol(libubi_t desc, const char *node, int vol_id, long long bytes)
1118 {
1119         int fd, ret;
1120         struct ubi_rsvol_req req;
1121
1122         desc = desc;
1123         fd = open(node, O_RDONLY);
1124         if (fd == -1) {
1125                 sys_errmsg("cannot open \"%s\"", node);
1126                 return -1;
1127         }
1128         req.bytes = bytes;
1129         req.vol_id = vol_id;
1130
1131         ret = ioctl(fd, UBI_IOCRSVOL, &req);
1132         close(fd);
1133         return ret;
1134 }
1135
1136 int ubi_update_start(libubi_t desc, int fd, long long bytes)
1137 {
1138         desc = desc;
1139         if (ioctl(fd, UBI_IOCVOLUP, &bytes))
1140                 return -1;
1141         return 0;
1142 }
1143
1144 int ubi_leb_change_start(libubi_t desc, int fd, int lnum, int bytes)
1145 {
1146         struct ubi_leb_change_req req;
1147
1148         desc = desc;
1149         memset(&req, 0, sizeof(struct ubi_leb_change_req));
1150         req.lnum = lnum;
1151         req.bytes = bytes;
1152         req.dtype = 3;
1153
1154         if (ioctl(fd, UBI_IOCEBCH, &req))
1155                 return -1;
1156         return 0;
1157 }
1158
1159 int ubi_dev_present(libubi_t desc, int dev_num)
1160 {
1161         struct stat st;
1162         struct libubi *lib = (struct libubi *)desc;
1163         char file[strlen(lib->ubi_dev) + 50];
1164
1165         sprintf(file, lib->ubi_dev, dev_num);
1166         return !stat(file, &st);
1167 }
1168
1169 int ubi_get_dev_info1(libubi_t desc, int dev_num, struct ubi_dev_info *info)
1170 {
1171         DIR *sysfs_ubi;
1172         struct dirent *dirent;
1173         struct libubi *lib = (struct libubi *)desc;
1174
1175         memset(info, 0, sizeof(struct ubi_dev_info));
1176         info->dev_num = dev_num;
1177
1178         if (!ubi_dev_present(desc, dev_num))
1179                 return -1;
1180
1181         sysfs_ubi = opendir(lib->sysfs_ubi);
1182         if (!sysfs_ubi)
1183                 return -1;
1184
1185         info->lowest_vol_id = INT_MAX;
1186
1187         while (1) {
1188                 int vol_id, ret, devno;
1189                 char tmp_buf[256];
1190
1191                 errno = 0;
1192                 dirent = readdir(sysfs_ubi);
1193                 if (!dirent)
1194                         break;
1195
1196                 if (strlen(dirent->d_name) >= 255) {
1197                         errmsg("invalid entry in %s: \"%s\"",
1198                                lib->sysfs_ubi, dirent->d_name);
1199                         goto out_close;
1200                 }
1201
1202                 ret = sscanf(dirent->d_name, UBI_VOL_NAME_PATT"%s", &devno, &vol_id, tmp_buf);
1203                 if (ret == 2 && devno == dev_num) {
1204                         info->vol_count += 1;
1205                         if (vol_id > info->highest_vol_id)
1206                                 info->highest_vol_id = vol_id;
1207                         if (vol_id < info->lowest_vol_id)
1208                                 info->lowest_vol_id = vol_id;
1209                 }
1210         }
1211
1212         if (!dirent && errno) {
1213                 sys_errmsg("readdir failed on \"%s\"", lib->sysfs_ubi);
1214                 goto out_close;
1215         }
1216
1217         if (closedir(sysfs_ubi)) {
1218                 sys_errmsg("closedir failed on \"%s\"", lib->sysfs_ubi);
1219                 return -1;
1220         }
1221         if (info->lowest_vol_id == INT_MAX)
1222                 info->lowest_vol_id = 0;
1223
1224         if (dev_get_major(lib, dev_num, &info->major, &info->minor))
1225                 return -1;
1226
1227         if (dev_read_int(lib->dev_mtd_num, dev_num, &info->mtd_num))
1228                 return -1;
1229         if (dev_read_int(lib->dev_avail_ebs, dev_num, &info->avail_lebs))
1230                 return -1;
1231         if (dev_read_int(lib->dev_total_ebs, dev_num, &info->total_lebs))
1232                 return -1;
1233         if (dev_read_int(lib->dev_bad_count, dev_num, &info->bad_count))
1234                 return -1;
1235         if (dev_read_int(lib->dev_eb_size, dev_num, &info->leb_size))
1236                 return -1;
1237         if (dev_read_int(lib->dev_bad_rsvd, dev_num, &info->bad_rsvd))
1238                 return -1;
1239         if (dev_read_ll(lib->dev_max_ec, dev_num, &info->max_ec))
1240                 return -1;
1241         if (dev_read_int(lib->dev_max_vols, dev_num, &info->max_vol_count))
1242                 return -1;
1243         if (dev_read_int(lib->dev_min_io_size, dev_num, &info->min_io_size))
1244                 return -1;
1245
1246         info->avail_bytes = (long long)info->avail_lebs * info->leb_size;
1247         info->total_bytes = (long long)info->total_lebs * info->leb_size;
1248
1249         return 0;
1250
1251 out_close:
1252         closedir(sysfs_ubi);
1253         return -1;
1254 }
1255
1256 int ubi_get_dev_info(libubi_t desc, const char *node, struct ubi_dev_info *info)
1257 {
1258         int err, dev_num = 0;
1259         struct libubi *lib = (struct libubi *)desc;
1260
1261         err = ubi_probe_node(desc, node);
1262         if (err != 1) {
1263                 if (err == 2)
1264                         errno = ENODEV;
1265                 return -1;
1266         }
1267
1268         if (dev_node2num(lib, node, &dev_num))
1269                 return -1;
1270
1271         return ubi_get_dev_info1(desc, dev_num, info);
1272 }
1273
1274 int ubi_get_vol_info1(libubi_t desc, int dev_num, int vol_id,
1275                       struct ubi_vol_info *info)
1276 {
1277         int ret;
1278         struct libubi *lib = (struct libubi *)desc;
1279         char buf[50];
1280
1281         memset(info, 0, sizeof(struct ubi_vol_info));
1282         info->dev_num = dev_num;
1283         info->vol_id = vol_id;
1284
1285         if (vol_get_major(lib, dev_num, vol_id, &info->major, &info->minor))
1286                 return -1;
1287
1288         ret = vol_read_data(lib->vol_type, dev_num, vol_id, buf, 50);
1289         if (ret < 0)
1290                 return -1;
1291
1292         if (strncmp(buf, "static\n", ret) == 0)
1293                 info->type = UBI_STATIC_VOLUME;
1294         else if (strncmp(buf, "dynamic\n", ret) == 0)
1295                 info->type = UBI_DYNAMIC_VOLUME;
1296         else {
1297                 errmsg("bad value at \"%s\"", buf);
1298                 errno = EINVAL;
1299                 return -1;
1300         }
1301
1302         ret = vol_read_int(lib->vol_alignment, dev_num, vol_id,
1303                            &info->alignment);
1304         if (ret)
1305                 return -1;
1306         ret = vol_read_ll(lib->vol_data_bytes, dev_num, vol_id,
1307                           &info->data_bytes);
1308         if (ret)
1309                 return -1;
1310         ret = vol_read_int(lib->vol_rsvd_ebs, dev_num, vol_id, &info->rsvd_lebs);
1311         if (ret)
1312                 return -1;
1313         ret = vol_read_int(lib->vol_eb_size, dev_num, vol_id, &info->leb_size);
1314         if (ret)
1315                 return -1;
1316         ret = vol_read_int(lib->vol_corrupted, dev_num, vol_id,
1317                            &info->corrupted);
1318         if (ret)
1319                 return -1;
1320         info->rsvd_bytes = (long long)info->leb_size * info->rsvd_lebs;
1321
1322         ret = vol_read_data(lib->vol_name, dev_num, vol_id, &info->name,
1323                             UBI_VOL_NAME_MAX + 2);
1324         if (ret < 0)
1325                 return -1;
1326
1327         info->name[ret - 1] = '\0';
1328         return 0;
1329 }
1330
1331 int ubi_get_vol_info(libubi_t desc, const char *node, struct ubi_vol_info *info)
1332 {
1333         int err, vol_id = 0, dev_num = 0;
1334         struct libubi *lib = (struct libubi *)desc;
1335
1336         err = ubi_probe_node(desc, node);
1337         if (err != 2) {
1338                 if (err == 1)
1339                         errno = ENODEV;
1340                 return -1;
1341         }
1342
1343         if (vol_node2nums(lib, node, &dev_num, &vol_id))
1344                 return -1;
1345
1346         return ubi_get_vol_info1(desc, dev_num, vol_id, info);
1347 }
1348
1349 int ubi_get_vol_info1_nm(libubi_t desc, int dev_num, const char *name,
1350                          struct ubi_vol_info *info)
1351 {
1352         int i, err;
1353         unsigned int nlen = strlen(name);
1354         struct ubi_dev_info dev_info;
1355
1356         if (nlen == 0) {
1357                 errmsg("bad \"name\" input parameter");
1358                 errno = EINVAL;
1359                 return -1;
1360         }
1361
1362         err = ubi_get_dev_info1(desc, dev_num, &dev_info);
1363         if (err)
1364                 return err;
1365
1366         for (i = dev_info.lowest_vol_id;
1367              i <= dev_info.highest_vol_id; i++) {
1368                 err = ubi_get_vol_info1(desc, dev_num, i, info);
1369                 if (err == -1) {
1370                         if (errno == ENOENT)
1371                                 continue;
1372                         return -1;
1373                 }
1374
1375                 if (nlen == strlen(info->name) && !strcmp(name, info->name))
1376                         return 0;
1377         }
1378
1379         errno = ENOENT;
1380         return -1;
1381 }
1382
1383 int ubi_set_property(int fd, uint8_t property, uint64_t value)
1384 {
1385         struct ubi_set_vol_prop_req r;
1386
1387         memset(&r, 0, sizeof(struct ubi_set_vol_prop_req));
1388         r.property = property;
1389         r.value = value;
1390
1391         return ioctl(fd, UBI_IOCSETVOLPROP, &r);
1392 }
1393
1394 int ubi_leb_unmap(int fd, int lnum)
1395 {
1396         return ioctl(fd, UBI_IOCEBUNMAP, &lnum);
1397 }
1398
1399 int ubi_is_mapped(int fd, int lnum)
1400 {
1401         return ioctl(fd, UBI_IOCEBISMAP, &lnum);
1402 }