-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathmkfs.ubifs.c
2397 lines (2128 loc) · 63.1 KB
/
mkfs.ubifs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2008 Nokia Corporation.
* Copyright (C) 2008 University of Szeged, Hungary
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors: Adrian Hunter
* Artem Bityutskiy
* Zoltan Sogor
*/
#include "mkfs.ubifs.h"
#include <crc32.h>
#define PROGRAM_VERSION "1.5"
/* Size (prime number) of hash table for link counting */
#define HASH_TABLE_SIZE 10099
/* The node buffer must allow for worst case compression */
#define NODE_BUFFER_SIZE (UBIFS_DATA_NODE_SZ + \
UBIFS_BLOCK_SIZE * WORST_COMPR_FACTOR)
/* Default time granularity in nanoseconds */
#define DEFAULT_TIME_GRAN 1000000000
/**
* struct idx_entry - index entry.
* @next: next index entry (NULL at end of list)
* @prev: previous index entry (NULL at beginning of list)
* @key: key
* @name: directory entry name used for sorting colliding keys by name
* @lnum: LEB number
* @offs: offset
* @len: length
*
* The index is recorded as a linked list which is sorted and used to create
* the bottom level of the on-flash index tree. The remaining levels of the
* index tree are each built from the level below.
*/
struct idx_entry {
struct idx_entry *next;
struct idx_entry *prev;
union ubifs_key key;
char *name;
int lnum;
int offs;
int len;
};
/**
* struct inum_mapping - inode number mapping for link counting.
* @next: next inum_mapping (NULL at end of list)
* @prev: previous inum_mapping (NULL at beginning of list)
* @dev: source device on which the source inode number resides
* @inum: source inode number of the file
* @use_inum: target inode number of the file
* @use_nlink: number of links
* @path_name: a path name of the file
* @st: struct stat object containing inode attributes which have to be used
* when the inode is being created (actually only UID, GID, access
* mode, major and minor device numbers)
*
* If a file has more than one hard link, then the number of hard links that
* exist in the source directory hierarchy must be counted to exclude the
* possibility that the file is linked from outside the source directory
* hierarchy.
*
* The inum_mappings are stored in a hash_table of linked lists.
*/
struct inum_mapping {
struct inum_mapping *next;
struct inum_mapping *prev;
dev_t dev;
ino_t inum;
ino_t use_inum;
unsigned int use_nlink;
char *path_name;
struct stat st;
};
/*
* Because we copy functions from the kernel, we use a subset of the UBIFS
* file-system description object struct ubifs_info.
*/
struct ubifs_info info_;
static struct ubifs_info *c = &info_;
static libubi_t ubi;
/* Debug levels are: 0 (none), 1 (statistics), 2 (files) ,3 (more details) */
int debug_level;
int verbose;
static char *root;
static int root_len;
static struct stat root_st;
static char *output;
static int out_fd;
static int out_ubi;
static int squash_owner;
static int squash_rino_perm;
/* The 'head' (position) which nodes are written */
static int head_lnum;
static int head_offs;
static int head_flags;
/* The index list */
static struct idx_entry *idx_list_first;
static struct idx_entry *idx_list_last;
static size_t idx_cnt;
/* Global buffers */
static void *leb_buf;
static void *node_buf;
static void *block_buf;
/* Hash table for inode link counting */
static struct inum_mapping **hash_table;
/* Inode creation sequence number */
static unsigned long long creat_sqnum;
static const char *optstring = "d:r:m:o:D:h?vVe:c:g:f:Fp:k:x:X:j:R:l:j:UQq";
static const struct option longopts[] = {
{"root", 1, NULL, 'r'},
{"min-io-size", 1, NULL, 'm'},
{"leb-size", 1, NULL, 'e'},
{"max-leb-cnt", 1, NULL, 'c'},
{"output", 1, NULL, 'o'},
{"devtable", 1, NULL, 'D'},
{"help", 0, NULL, 'h'},
{"verbose", 0, NULL, 'v'},
{"version", 0, NULL, 'V'},
{"debug-level", 1, NULL, 'g'},
{"jrn-size", 1, NULL, 'j'},
{"reserved", 1, NULL, 'R'},
{"compr", 1, NULL, 'x'},
{"favor-percent", 1, NULL, 'X'},
{"fanout", 1, NULL, 'f'},
{"space-fixup", 0, NULL, 'F'},
{"keyhash", 1, NULL, 'k'},
{"log-lebs", 1, NULL, 'l'},
{"orph-lebs", 1, NULL, 'p'},
{"squash-uids" , 0, NULL, 'U'},
{"squash-rino-perm", 0, NULL, 'Q'},
{"nosquash-rino-perm", 0, NULL, 'q'},
{NULL, 0, NULL, 0}
};
static const char *helptext =
"Usage: mkfs.ubifs [OPTIONS] target\n"
"Make a UBIFS file system image from an existing directory tree\n\n"
"Examples:\n"
"Build file system from directory /opt/img, writting the result in the ubifs.img file\n"
"\tmkfs.ubifs -m 512 -e 128KiB -c 100 -r /opt/img ubifs.img\n"
"The same, but writting directly to an UBI volume\n"
"\tmkfs.ubifs -r /opt/img /dev/ubi0_0\n"
"Creating an empty UBIFS filesystem on an UBI volume\n"
"\tmkfs.ubifs /dev/ubi0_0\n\n"
"Options:\n"
"-r, -d, --root=DIR build file system from directory DIR\n"
"-m, --min-io-size=SIZE minimum I/O unit size\n"
"-e, --leb-size=SIZE logical erase block size\n"
"-c, --max-leb-cnt=COUNT maximum logical erase block count\n"
"-o, --output=FILE output to FILE\n"
"-j, --jrn-size=SIZE journal size\n"
"-R, --reserved=SIZE how much space should be reserved for the super-user\n"
"-x, --compr=TYPE compression type - \"lzo\", \"favor_lzo\", \"zlib\" or\n"
" \"none\" (default: \"lzo\")\n"
"-X, --favor-percent may only be used with favor LZO compression and defines\n"
" how many percent better zlib should compress to make\n"
" mkfs.ubifs use zlib instead of LZO (default 20%)\n"
"-f, --fanout=NUM fanout NUM (default: 8)\n"
"-F, --space-fixup file-system free space has to be fixed up on first mount\n"
" (requires kernel version 3.0 or greater)\n"
"-k, --keyhash=TYPE key hash type - \"r5\" or \"test\" (default: \"r5\")\n"
"-p, --orph-lebs=COUNT count of erase blocks for orphans (default: 1)\n"
"-D, --devtable=FILE use device table FILE\n"
"-U, --squash-uids squash owners making all files owned by root\n"
"-l, --log-lebs=COUNT count of erase blocks for the log (used only for\n"
" debugging)\n"
"-v, --verbose verbose operation\n"
"-V, --version display version information\n"
"-g, --debug=LEVEL display debug information (0 - none, 1 - statistics,\n"
" 2 - files, 3 - more details)\n"
"-Q, --squash-rino-perm ignore permissions of the FS image directory (the one\n"
" specified with --root) and make the UBIFS root inode\n"
" permissions to be {uid=gid=root, u+rwx,go+rx}; this is\n"
" a legacy compatibility option and it will be removed\n"
" at some point, do not use it\n"
"-q, --nosquash-rino-perm for the UBIFS root inode use permissions of the FS\n"
" image directory (the one specified with --root); this\n"
" is the default behavior; this option will be removed\n"
" at some point, do not use it, see clarifications below;\n"
"-h, --help display this help text\n\n"
"Note, SIZE is specified in bytes, but it may also be specified in Kilobytes,\n"
"Megabytes, and Gigabytes if a KiB, MiB, or GiB suffix is used.\n\n"
"If you specify \"lzo\" or \"zlib\" compressors, mkfs.ubifs will use this compressor\n"
"for all data. The \"none\" disables any data compression. The \"favor_lzo\" is not\n"
"really a separate compressor. It is just a method of combining \"lzo\" and \"zlib\"\n"
"compressors. Namely, mkfs.ubifs tries to compress data with both \"lzo\" and \"zlib\"\n"
"compressors, then it compares which compressor is better. If \"zlib\" compresses 20\n"
"or more percent better than \"lzo\", mkfs.ubifs chooses \"lzo\", otherwise it chooses\n"
"\"zlib\". The \"--favor-percent\" may specify arbitrary threshold instead of the\n"
"default 20%.\n\n"
"The -R parameter specifies amount of bytes reserved for the super-user.\n\n"
"Some clarifications about --squash-rino-perm and --nosquash-rino-perm options.\n"
"Originally, mkfs.ubifs did not have them, and it always set permissions for the UBIFS\n"
"root inode to be {uid=gid=root, u+rwx,go+rx}. This was a bug which was found too\n"
"late, when mkfs.ubifs had already been used in production. To fix this bug, 2 new\n"
"options were introduced: --squash-rino-perm which preserves the old behavior and\n"
"--nosquash-rino-perm which makes mkfs.ubifs use the right permissions for the root\n"
"inode. Now these options are considered depricated and they will be removed later, so\n"
"do not use them.\n\n"
"The -F parameter is used to set the \"fix up free space\" flag in the superblock,\n"
"which forces UBIFS to \"fixup\" all the free space which it is going to use. This\n"
"option is useful to work-around the problem of double free space programming: if the\n"
"flasher program which flashes the UBI image is unable to skip NAND pages containing\n"
"only 0xFF bytes, the effect is that some NAND pages are written to twice - first time\n"
"when flashing the image and the second time when UBIFS is mounted and writes useful\n"
"data there. A proper UBI-aware flasher should skip such NAND pages, though. Note, this\n"
"flag may make the first mount very slow, because the \"free space fixup\" procedure\n"
"takes time. This feature is supported by the Linux kernel starting from version 3.0.\n";
/**
* make_path - make a path name from a directory and a name.
* @dir: directory path name
* @name: name
*/
static char *make_path(const char *dir, const char *name)
{
char *s;
s = malloc(strlen(dir) + strlen(name) + 2);
if (!s)
return NULL;
strcpy(s, dir);
if (dir[strlen(dir) - 1] != '/')
strcat(s, "/");
strcat(s, name);
return s;
}
/**
* same_dir - determine if two file descriptors refer to the same directory.
* @fd1: file descriptor 1
* @fd2: file descriptor 2
*/
static int same_dir(int fd1, int fd2)
{
struct stat stat1, stat2;
if (fstat(fd1, &stat1) == -1)
return -1;
if (fstat(fd2, &stat2) == -1)
return -1;
return stat1.st_dev == stat2.st_dev && stat1.st_ino == stat2.st_ino;
}
/**
* do_openat - open a file in a directory.
* @fd: file descriptor of open directory
* @path: path relative to directory
* @flags: open flags
*
* This function is provided because the library function openat is sometimes
* not available.
*/
static int do_openat(int fd, const char *path, int flags)
{
int ret;
char *cwd;
cwd = getcwd(NULL, 0);
if (!cwd)
return -1;
ret = fchdir(fd);
if (ret != -1)
ret = open(path, flags);
if (chdir(cwd) && !ret)
ret = -1;
free(cwd);
return ret;
}
/**
* in_path - determine if a file is beneath a directory.
* @dir_name: directory path name
* @file_name: file path name
*/
static int in_path(const char *dir_name, const char *file_name)
{
char *fn = strdup(file_name);
char *dn;
int fd1, fd2, fd3, ret = -1, top_fd;
if (!fn)
return -1;
top_fd = open("/", O_RDONLY);
if (top_fd != -1) {
dn = dirname(fn);
fd1 = open(dir_name, O_RDONLY);
if (fd1 != -1) {
fd2 = open(dn, O_RDONLY);
if (fd2 != -1) {
while (1) {
int same;
same = same_dir(fd1, fd2);
if (same) {
ret = same;
break;
}
if (same_dir(fd2, top_fd)) {
ret = 0;
break;
}
fd3 = do_openat(fd2, "..", O_RDONLY);
if (fd3 == -1)
break;
close(fd2);
fd2 = fd3;
}
close(fd2);
}
close(fd1);
}
close(top_fd);
}
free(fn);
return ret;
}
/**
* calc_min_log_lebs - calculate the minimum number of log LEBs needed.
* @max_bud_bytes: journal size (buds only)
*/
static int calc_min_log_lebs(unsigned long long max_bud_bytes)
{
int buds, log_lebs;
unsigned long long log_size;
buds = (max_bud_bytes + c->leb_size - 1) / c->leb_size;
log_size = ALIGN(UBIFS_REF_NODE_SZ, c->min_io_size);
log_size *= buds;
log_size += ALIGN(UBIFS_CS_NODE_SZ +
UBIFS_REF_NODE_SZ * (c->jhead_cnt + 2),
c->min_io_size);
log_lebs = (log_size + c->leb_size - 1) / c->leb_size;
log_lebs += 1;
return log_lebs;
}
/**
* add_space_overhead - add UBIFS overhead.
* @size: flash space which should be visible to the user
*
* UBIFS has overhead, and if we need to reserve @size bytes for the user data,
* we have to reserve more flash space, to compensate the overhead. This
* function calculates and returns the amount of physical flash space which
* should be reserved to provide @size bytes for the user.
*/
static long long add_space_overhead(long long size)
{
int divisor, factor, f, max_idx_node_sz;
/*
* Do the opposite to what the 'ubifs_reported_space()' kernel UBIFS
* function does.
*/
max_idx_node_sz = ubifs_idx_node_sz(c, c->fanout);
f = c->fanout > 3 ? c->fanout >> 1 : 2;
divisor = UBIFS_BLOCK_SIZE;
factor = UBIFS_MAX_DATA_NODE_SZ;
factor += (max_idx_node_sz * 3) / (f - 1);
size *= factor;
return size / divisor;
}
static inline int is_power_of_2(unsigned long long n)
{
return (n != 0 && ((n & (n - 1)) == 0));
}
static int validate_options(void)
{
int tmp;
if (!output)
return err_msg("no output file or UBI volume specified");
if (root && in_path(root, output))
return err_msg("output file cannot be in the UBIFS root "
"directory");
if (!is_power_of_2(c->min_io_size))
return err_msg("min. I/O unit size should be power of 2");
if (c->leb_size < c->min_io_size)
return err_msg("min. I/O unit cannot be larger than LEB size");
if (c->leb_size < UBIFS_MIN_LEB_SZ)
return err_msg("too small LEB size %d, minimum is %d",
c->min_io_size, UBIFS_MIN_LEB_SZ);
if (c->leb_size % c->min_io_size)
return err_msg("LEB should be multiple of min. I/O units");
if (c->leb_size % 8)
return err_msg("LEB size has to be multiple of 8");
if (c->leb_size > 1024*1024)
return err_msg("too large LEB size %d", c->leb_size);
if (c->max_leb_cnt < UBIFS_MIN_LEB_CNT)
return err_msg("too low max. count of LEBs, minimum is %d",
UBIFS_MIN_LEB_CNT);
if (c->fanout < UBIFS_MIN_FANOUT)
return err_msg("too low fanout, minimum is %d",
UBIFS_MIN_FANOUT);
tmp = c->leb_size - UBIFS_IDX_NODE_SZ;
tmp /= UBIFS_BRANCH_SZ + UBIFS_MAX_KEY_LEN;
if (c->fanout > tmp)
return err_msg("too high fanout, maximum is %d", tmp);
if (c->log_lebs < UBIFS_MIN_LOG_LEBS)
return err_msg("too few log LEBs, minimum is %d",
UBIFS_MIN_LOG_LEBS);
if (c->log_lebs >= c->max_leb_cnt - UBIFS_MIN_LEB_CNT)
return err_msg("too many log LEBs, maximum is %d",
c->max_leb_cnt - UBIFS_MIN_LEB_CNT);
if (c->orph_lebs < UBIFS_MIN_ORPH_LEBS)
return err_msg("too few orphan LEBs, minimum is %d",
UBIFS_MIN_ORPH_LEBS);
if (c->orph_lebs >= c->max_leb_cnt - UBIFS_MIN_LEB_CNT)
return err_msg("too many orphan LEBs, maximum is %d",
c->max_leb_cnt - UBIFS_MIN_LEB_CNT);
tmp = UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs + c->lpt_lebs;
tmp += c->orph_lebs + 4;
if (tmp > c->max_leb_cnt)
return err_msg("too low max. count of LEBs, expected at "
"least %d", tmp);
tmp = calc_min_log_lebs(c->max_bud_bytes);
if (c->log_lebs < calc_min_log_lebs(c->max_bud_bytes))
return err_msg("too few log LEBs, expected at least %d", tmp);
if (c->rp_size >= ((long long)c->leb_size * c->max_leb_cnt) / 2)
return err_msg("too much reserved space %lld", c->rp_size);
return 0;
}
/**
* get_multiplier - convert size specifier to an integer multiplier.
* @str: the size specifier string
*
* This function parses the @str size specifier, which may be one of
* 'KiB', 'MiB', or 'GiB' into an integer multiplier. Returns positive
* size multiplier in case of success and %-1 in case of failure.
*/
static int get_multiplier(const char *str)
{
if (!str)
return 1;
/* Remove spaces before the specifier */
while (*str == ' ' || *str == '\t')
str += 1;
if (!strcmp(str, "KiB"))
return 1024;
if (!strcmp(str, "MiB"))
return 1024 * 1024;
if (!strcmp(str, "GiB"))
return 1024 * 1024 * 1024;
return -1;
}
/**
* get_bytes - convert a string containing amount of bytes into an
* integer.
* @str: string to convert
*
* This function parses @str which may have one of 'KiB', 'MiB', or 'GiB' size
* specifiers. Returns positive amount of bytes in case of success and %-1 in
* case of failure.
*/
static long long get_bytes(const char *str)
{
char *endp;
long long bytes = strtoull(str, &endp, 0);
if (endp == str || bytes < 0)
return err_msg("incorrect amount of bytes: \"%s\"", str);
if (*endp != '\0') {
int mult = get_multiplier(endp);
if (mult == -1)
return err_msg("bad size specifier: \"%s\" - "
"should be 'KiB', 'MiB' or 'GiB'", endp);
bytes *= mult;
}
return bytes;
}
/**
* open_ubi - open the UBI volume.
* @node: name of the UBI volume character device to fetch information about
*
* Returns %0 in case of success and %-1 in case of failure
*/
static int open_ubi(const char *node)
{
struct stat st;
if (stat(node, &st) || !S_ISCHR(st.st_mode))
return -1;
ubi = libubi_open();
if (!ubi)
return -1;
if (ubi_get_vol_info(ubi, node, &c->vi))
return -1;
if (ubi_get_dev_info1(ubi, c->vi.dev_num, &c->di))
return -1;
return 0;
}
static int get_options(int argc, char**argv)
{
int opt, i;
const char *tbl_file = NULL;
struct stat st;
char *endp;
c->fanout = 8;
c->orph_lebs = 1;
c->key_hash = key_r5_hash;
c->key_len = UBIFS_SK_LEN;
c->default_compr = UBIFS_COMPR_LZO;
c->favor_percent = 20;
c->lsave_cnt = 256;
c->leb_size = -1;
c->min_io_size = -1;
c->max_leb_cnt = -1;
c->max_bud_bytes = -1;
c->log_lebs = -1;
while (1) {
opt = getopt_long(argc, argv, optstring, longopts, &i);
if (opt == -1)
break;
switch (opt) {
case 'r':
case 'd':
root_len = strlen(optarg);
root = malloc(root_len + 2);
if (!root)
return err_msg("cannot allocate memory");
/*
* The further code expects '/' at the end of the root
* UBIFS directory on the host.
*/
memcpy(root, optarg, root_len);
if (root[root_len - 1] != '/')
root[root_len++] = '/';
root[root_len] = 0;
/* Make sure the root directory exists */
if (stat(root, &st))
return sys_err_msg("bad root directory '%s'",
root);
break;
case 'm':
c->min_io_size = get_bytes(optarg);
if (c->min_io_size <= 0)
return err_msg("bad min. I/O size");
break;
case 'e':
c->leb_size = get_bytes(optarg);
if (c->leb_size <= 0)
return err_msg("bad LEB size");
break;
case 'c':
c->max_leb_cnt = get_bytes(optarg);
if (c->max_leb_cnt <= 0)
return err_msg("bad maximum LEB count");
break;
case 'o':
output = strdup(optarg);
break;
case 'D':
tbl_file = optarg;
if (stat(tbl_file, &st) < 0)
return sys_err_msg("bad device table file '%s'",
tbl_file);
break;
case 'h':
case '?':
printf("%s", helptext);
exit(0);
case 'v':
verbose = 1;
break;
case 'V':
printf("Version " PROGRAM_VERSION "\n");
exit(0);
case 'g':
debug_level = strtol(optarg, &endp, 0);
if (*endp != '\0' || endp == optarg ||
debug_level < 0 || debug_level > 3)
return err_msg("bad debugging level '%s'",
optarg);
break;
case 'f':
c->fanout = strtol(optarg, &endp, 0);
if (*endp != '\0' || endp == optarg || c->fanout <= 0)
return err_msg("bad fanout %s", optarg);
break;
case 'F':
c->space_fixup = 1;
break;
case 'l':
c->log_lebs = strtol(optarg, &endp, 0);
if (*endp != '\0' || endp == optarg || c->log_lebs <= 0)
return err_msg("bad count of log LEBs '%s'",
optarg);
break;
case 'p':
c->orph_lebs = strtol(optarg, &endp, 0);
if (*endp != '\0' || endp == optarg ||
c->orph_lebs <= 0)
return err_msg("bad orphan LEB count '%s'",
optarg);
break;
case 'k':
if (strcmp(optarg, "r5") == 0) {
c->key_hash = key_r5_hash;
c->key_hash_type = UBIFS_KEY_HASH_R5;
} else if (strcmp(optarg, "test") == 0) {
c->key_hash = key_test_hash;
c->key_hash_type = UBIFS_KEY_HASH_TEST;
} else
return err_msg("bad key hash");
break;
case 'x':
if (strcmp(optarg, "favor_lzo") == 0)
c->favor_lzo = 1;
else if (strcmp(optarg, "zlib") == 0)
c->default_compr = UBIFS_COMPR_ZLIB;
else if (strcmp(optarg, "none") == 0)
c->default_compr = UBIFS_COMPR_NONE;
else if (strcmp(optarg, "lzo") != 0)
return err_msg("bad compressor name");
break;
case 'X':
c->favor_percent = strtol(optarg, &endp, 0);
if (*endp != '\0' || endp == optarg ||
c->favor_percent <= 0 || c->favor_percent >= 100)
return err_msg("bad favor LZO percent '%s'",
optarg);
break;
case 'j':
c->max_bud_bytes = get_bytes(optarg);
if (c->max_bud_bytes <= 0)
return err_msg("bad maximum amount of buds");
break;
case 'R':
c->rp_size = get_bytes(optarg);
if (c->rp_size < 0)
return err_msg("bad reserved bytes count");
break;
case 'U':
squash_owner = 1;
break;
case 'Q':
squash_rino_perm = 1;
printf("WARNING: --squash-rino-perm is depricated, do not use it\n");
break;
case 'q':
printf("WARNING: --nosquash-rino-perm is depricated, do not use it\n");
break;
}
}
if (optind != argc && !output)
output = strdup(argv[optind]);
if (!output)
return err_msg("not output device or file specified");
out_ubi = !open_ubi(output);
if (out_ubi) {
c->min_io_size = c->di.min_io_size;
c->leb_size = c->vi.leb_size;
c->max_leb_cnt = c->vi.rsvd_lebs;
}
if (c->min_io_size == -1)
return err_msg("min. I/O unit was not specified "
"(use -h for help)");
if (c->leb_size == -1)
return err_msg("LEB size was not specified (use -h for help)");
if (c->max_leb_cnt == -1)
return err_msg("Maximum count of LEBs was not specified "
"(use -h for help)");
if (squash_rino_perm != -1 && !root)
return err_msg("--squash-rino-perm and nosquash-rino-perm options "
"can be used only with the --root option");
if (c->max_bud_bytes == -1) {
int lebs;
lebs = c->max_leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
lebs -= c->orph_lebs;
if (c->log_lebs != -1)
lebs -= c->log_lebs;
else
lebs -= UBIFS_MIN_LOG_LEBS;
/*
* We do not know lprops geometry so far, so assume minimum
* count of lprops LEBs.
*/
lebs -= UBIFS_MIN_LPT_LEBS;
/* Make the journal about 12.5% of main area lebs */
c->max_bud_bytes = (lebs / 8) * (long long)c->leb_size;
/* Make the max journal size 8MiB */
if (c->max_bud_bytes > 8 * 1024 * 1024)
c->max_bud_bytes = 8 * 1024 * 1024;
if (c->max_bud_bytes < 4 * c->leb_size)
c->max_bud_bytes = 4 * c->leb_size;
}
if (c->log_lebs == -1) {
c->log_lebs = calc_min_log_lebs(c->max_bud_bytes);
c->log_lebs += 2;
}
if (c->min_io_size < 8)
c->min_io_size = 8;
c->rp_size = add_space_overhead(c->rp_size);
if (verbose) {
printf("mkfs.ubifs\n");
printf("\troot: %s\n", root);
printf("\tmin_io_size: %d\n", c->min_io_size);
printf("\tleb_size: %d\n", c->leb_size);
printf("\tmax_leb_cnt: %d\n", c->max_leb_cnt);
printf("\toutput: %s\n", output);
printf("\tjrn_size: %llu\n", c->max_bud_bytes);
printf("\treserved: %llu\n", c->rp_size);
switch (c->default_compr) {
case UBIFS_COMPR_LZO:
printf("\tcompr: lzo\n");
break;
case UBIFS_COMPR_ZLIB:
printf("\tcompr: zlib\n");
break;
case UBIFS_COMPR_NONE:
printf("\tcompr: none\n");
break;
}
printf("\tkeyhash: %s\n", (c->key_hash == key_r5_hash) ?
"r5" : "test");
printf("\tfanout: %d\n", c->fanout);
printf("\torph_lebs: %d\n", c->orph_lebs);
printf("\tspace_fixup: %d\n", c->space_fixup);
}
if (validate_options())
return -1;
if (tbl_file && parse_devtable(tbl_file))
return err_msg("cannot parse device table file '%s'", tbl_file);
return 0;
}
/**
* prepare_node - fill in the common header.
* @node: node
* @len: node length
*/
static void prepare_node(void *node, int len)
{
uint32_t crc;
struct ubifs_ch *ch = node;
ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
ch->len = cpu_to_le32(len);
ch->group_type = UBIFS_NO_NODE_GROUP;
ch->sqnum = cpu_to_le64(++c->max_sqnum);
ch->padding[0] = ch->padding[1] = 0;
crc = mtd_crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
ch->crc = cpu_to_le32(crc);
}
/**
* write_leb - copy the image of a LEB to the output target.
* @lnum: LEB number
* @len: length of data in the buffer
* @buf: buffer (must be at least c->leb_size bytes)
* @dtype: expected data type
*/
int write_leb(int lnum, int len, void *buf, int dtype)
{
off64_t pos = (off64_t)lnum * c->leb_size;
dbg_msg(3, "LEB %d len %d", lnum, len);
memset(buf + len, 0xff, c->leb_size - len);
if (out_ubi)
if (ubi_leb_change_start(ubi, out_fd, lnum, c->leb_size, dtype))
return sys_err_msg("ubi_leb_change_start failed");
if (lseek64(out_fd, pos, SEEK_SET) != pos)
return sys_err_msg("lseek64 failed seeking %lld",
(long long)pos);
if (write(out_fd, buf, c->leb_size) != c->leb_size)
return sys_err_msg("write failed writing %d bytes at pos %lld",
c->leb_size, (long long)pos);
return 0;
}
/**
* write_empty_leb - copy the image of an empty LEB to the output target.
* @lnum: LEB number
* @dtype: expected data type
*/
static int write_empty_leb(int lnum, int dtype)
{
return write_leb(lnum, 0, leb_buf, dtype);
}
/**
* do_pad - pad a buffer to the minimum I/O size.
* @buf: buffer
* @len: buffer length
*/
static int do_pad(void *buf, int len)
{
int pad_len, alen = ALIGN(len, 8), wlen = ALIGN(alen, c->min_io_size);
uint32_t crc;
memset(buf + len, 0xff, alen - len);
pad_len = wlen - alen;
dbg_msg(3, "len %d pad_len %d", len, pad_len);
buf += alen;
if (pad_len >= (int)UBIFS_PAD_NODE_SZ) {
struct ubifs_ch *ch = buf;
struct ubifs_pad_node *pad_node = buf;
ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
ch->node_type = UBIFS_PAD_NODE;
ch->group_type = UBIFS_NO_NODE_GROUP;
ch->padding[0] = ch->padding[1] = 0;
ch->sqnum = cpu_to_le64(0);
ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ);
pad_len -= UBIFS_PAD_NODE_SZ;
pad_node->pad_len = cpu_to_le32(pad_len);
crc = mtd_crc32(UBIFS_CRC32_INIT, buf + 8,
UBIFS_PAD_NODE_SZ - 8);
ch->crc = cpu_to_le32(crc);
memset(buf + UBIFS_PAD_NODE_SZ, 0, pad_len);
} else if (pad_len > 0)
memset(buf, UBIFS_PADDING_BYTE, pad_len);
return wlen;
}
/**
* write_node - write a node to a LEB.
* @node: node
* @len: node length
* @lnum: LEB number
* @dtype: expected data type
*/
static int write_node(void *node, int len, int lnum, int dtype)
{
prepare_node(node, len);
memcpy(leb_buf, node, len);
len = do_pad(leb_buf, len);
return write_leb(lnum, len, leb_buf, dtype);
}
/**
* calc_dark - calculate LEB dark space size.
* @c: the UBIFS file-system description object
* @spc: amount of free and dirty space in the LEB
*
* This function calculates amount of dark space in an LEB which has @spc bytes
* of free and dirty space. Returns the calculations result.
*
* Dark space is the space which is not always usable - it depends on which
* nodes are written in which order. E.g., if an LEB has only 512 free bytes,
* it is dark space, because it cannot fit a large data node. So UBIFS cannot
* count on this LEB and treat these 512 bytes as usable because it is not true
* if, for example, only big chunks of uncompressible data will be written to
* the FS.
*/
static int calc_dark(struct ubifs_info *c, int spc)
{
if (spc < c->dark_wm)
return spc;
/*
* If we have slightly more space then the dark space watermark, we can
* anyway safely assume it we'll be able to write a node of the
* smallest size there.
*/
if (spc - c->dark_wm < (int)MIN_WRITE_SZ)
return spc - MIN_WRITE_SZ;
return c->dark_wm;
}
/**
* set_lprops - set the LEB property values for a LEB.
* @lnum: LEB number
* @offs: end offset of data in the LEB
* @flags: LEB property flags
*/
static void set_lprops(int lnum, int offs, int flags)
{
int i = lnum - c->main_first, free, dirty;
int a = max_t(int, c->min_io_size, 8);
free = c->leb_size - ALIGN(offs, a);
dirty = c->leb_size - free - ALIGN(offs, 8);
dbg_msg(3, "LEB %d free %d dirty %d flags %d", lnum, free, dirty,
flags);
if (i < c->main_lebs) {
c->lpt[i].free = free;
c->lpt[i].dirty = dirty;
c->lpt[i].flags = flags;
}
c->lst.total_free += free;
c->lst.total_dirty += dirty;
if (flags & LPROPS_INDEX)
c->lst.idx_lebs += 1;
else {
int spc;
spc = free + dirty;
if (spc < c->dead_wm)
c->lst.total_dead += spc;
else
c->lst.total_dark += calc_dark(c, spc);
c->lst.total_used += c->leb_size - spc;
}
}
/**
* add_to_index - add a node key and position to the index.
* @key: node key
* @lnum: node LEB number
* @offs: node offset
* @len: node length
*/
static int add_to_index(union ubifs_key *key, char *name, int lnum, int offs,
int len)
{
struct idx_entry *e;
dbg_msg(3, "LEB %d offs %d len %d", lnum, offs, len);
e = malloc(sizeof(struct idx_entry));
if (!e)
return err_msg("out of memory");
e->next = NULL;
e->prev = idx_list_last;
e->key = *key;
e->name = name;
e->lnum = lnum;
e->offs = offs;
e->len = len;
if (!idx_list_first)
idx_list_first = e;
if (idx_list_last)
idx_list_last->next = e;
idx_list_last = e;
idx_cnt += 1;
return 0;
}