• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * main.c
3  *
4  * Copyright (c) 2013 Samsung Electronics Co., Ltd.
5  *             http://www.samsung.com/
6  * Copyright (c) 2015 Jaegeuk Kim <jaegeuk@kernel.org>
7  *  : implement defrag.f2fs
8  * Copyright (C) 2015 Huawei Ltd.
9  *   Hou Pengyang <houpengyang@huawei.com>
10  *   Liu Shuoran <liushuoran@huawei.com>
11  *   Jaegeuk Kim <jaegeuk@kernel.org>
12  *  : add sload.f2fs
13  * Copyright (c) 2019 Google Inc.
14  *   Robin Hsu <robinhsu@google.com>
15  *  : add cache layer
16  * Copyright (c) 2020 Google Inc.
17  *   Robin Hsu <robinhsu@google.com>
18  *  : add sload compression support
19  *
20  * This program is free software; you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License version 2 as
22  * published by the Free Software Foundation.
23  */
24 #include "fsck.h"
25 #include <libgen.h>
26 #include <ctype.h>
27 #include <time.h>
28 #include <getopt.h>
29 #include <stdbool.h>
30 #include "quotaio.h"
31 #include "compress.h"
32 
33 struct f2fs_fsck gfsck;
34 
35 INIT_FEATURE_TABLE;
36 
37 #ifdef WITH_SLOAD
absolute_path(const char * file)38 static char *absolute_path(const char *file)
39 {
40 	char *ret;
41 	char cwd[PATH_MAX];
42 
43 	if (file[0] != '/') {
44 		if (getcwd(cwd, PATH_MAX) == NULL) {
45 			fprintf(stderr, "Failed to getcwd\n");
46 			exit(EXIT_FAILURE);
47 		}
48 		ret = malloc(strlen(cwd) + 1 + strlen(file) + 1);
49 		if (ret)
50 			sprintf(ret, "%s/%s", cwd, file);
51 	} else
52 		ret = strdup(file);
53 	return ret;
54 }
55 #endif
56 
fsck_usage()57 void fsck_usage()
58 {
59 	MSG(0, "\nUsage: fsck.f2fs [options] device\n");
60 	MSG(0, "[options]:\n");
61 	MSG(0, "  -a check/fix potential corruption, reported by f2fs\n");
62 	MSG(0, "  -c <num-cache-entry>  set number of cache entries"
63 			" (default 0)\n");
64 	MSG(0, "  -m <max-hash-collision>  set max cache hash collision"
65 			" (default 16)\n");
66 	MSG(0, "  -C encoding[:flag1,flag2] Set options for enabling"
67 			" casefolding\n");
68 	MSG(0, "  -d debug level [default:0]\n");
69 	MSG(0, "  -f check/fix entire partition\n");
70 	MSG(0, "  -g add default options\n");
71 	MSG(0, "  -l show superblock/checkpoint\n");
72 	MSG(0, "  -M show a file map\n");
73 	MSG(0, "  -O feature1[feature2,feature3,...] e.g. \"encrypt\"\n");
74 	MSG(0, "  -p preen mode [default:0 the same as -a [0|1|2]]\n");
75 	MSG(0, "  -S sparse_mode\n");
76 	MSG(0, "  -t show directory tree\n");
77 	MSG(0, "  -q preserve quota limits\n");
78 	MSG(0, "  -y fix all the time\n");
79 	MSG(0, "  -V print the version number and exit\n");
80 	MSG(0, "  --dry-run do not really fix corruptions\n");
81 	MSG(0, "  --no-kernel-check skips detecting kernel change\n");
82 	MSG(0, "  --kernel-check checks kernel change\n");
83 	MSG(0, "  --debug-cache to debug cache when -c is used\n");
84 	exit(1);
85 }
86 
dump_usage()87 void dump_usage()
88 {
89 	MSG(0, "\nUsage: dump.f2fs [options] device\n");
90 	MSG(0, "[options]:\n");
91 	MSG(0, "  -d debug level [default:0]\n");
92 	MSG(0, "  -i inode no (hex)\n");
93 	MSG(0, "  -n [NAT dump nid from #1~#2 (decimal), for all 0~-1]\n");
94 	MSG(0, "  -M show a block map\n");
95 	MSG(0, "  -s [SIT dump segno from #1~#2 (decimal), for all 0~-1]\n");
96 	MSG(0, "  -S sparse_mode\n");
97 	MSG(0, "  -a [SSA dump segno from #1~#2 (decimal), for all 0~-1]\n");
98 	MSG(0, "  -b blk_addr (in 4KB)\n");
99 	MSG(0, "  -V print the version number and exit\n");
100 
101 	exit(1);
102 }
103 
defrag_usage()104 void defrag_usage()
105 {
106 	MSG(0, "\nUsage: defrag.f2fs [options] device\n");
107 	MSG(0, "[options]:\n");
108 	MSG(0, "  -d debug level [default:0]\n");
109 	MSG(0, "  -s start block address [default: main_blkaddr]\n");
110 	MSG(0, "  -S sparse_mode\n");
111 	MSG(0, "  -l length [default:512 (2MB)]\n");
112 	MSG(0, "  -t target block address [default: main_blkaddr + 2MB]\n");
113 	MSG(0, "  -i set direction as shrink [default: expand]\n");
114 	MSG(0, "  -V print the version number and exit\n");
115 	exit(1);
116 }
117 
resize_usage()118 void resize_usage()
119 {
120 	MSG(0, "\nUsage: resize.f2fs [options] device\n");
121 	MSG(0, "[options]:\n");
122 	MSG(0, "  -d debug level [default:0]\n");
123 	MSG(0, "  -i extended node bitmap, node ratio is 20%% by default\n");
124 	MSG(0, "  -s safe resize (Does not resize metadata)");
125 	MSG(0, "  -t target sectors [default: device size]\n");
126 	MSG(0, "  -V print the version number and exit\n");
127 	exit(1);
128 }
129 
sload_usage()130 void sload_usage()
131 {
132 	MSG(0, "\nUsage: sload.f2fs [options] device\n");
133 	MSG(0, "[options]:\n");
134 	MSG(0, "  -C fs_config\n");
135 	MSG(0, "  -f source directory [path of the source directory]\n");
136 	MSG(0, "  -p product out directory\n");
137 	MSG(0, "  -s file_contexts\n");
138 	MSG(0, "  -S sparse_mode\n");
139 	MSG(0, "  -t mount point [prefix of target fs path, default:/]\n");
140 	MSG(0, "  -T timestamp\n");
141 	MSG(0, "  -P preserve owner: user and group\n");
142 	MSG(0, "  -c enable compression (default allow policy)\n");
143 	MSG(0, "    ------------ Compression sub-options -----------------\n");
144 	MSG(0, "    -L <log-of-blocks-per-cluster>, default 2\n");
145 	MSG(0, "    -a <algorithm> compression algorithm, default LZ4\n");
146 	MSG(0, "    -x <ext> compress files except for these extensions.\n");
147 	MSG(0, "    -i <ext> compress files with these extensions only.\n");
148 	MSG(0, "    * -i or -x: use it many times for multiple extensions.\n");
149 	MSG(0, "    * -i and -x cannot be used together..\n");
150 	MSG(0, "    -m <num> min compressed blocks per cluster\n");
151 	MSG(0, "    -r read only (to release unused blocks) for compressed "
152 			"files\n");
153 	MSG(0, "    ------------------------------------------------------\n");
154 	MSG(0, "  -d debug level [default:0]\n");
155 	MSG(0, "  -V print the version number and exit\n");
156 	exit(1);
157 }
158 
label_usage()159 void label_usage()
160 {
161 	MSG(0, "\nUsage: f2fslabel [options] device [volume-label]\n");
162 	MSG(0, "[options]:\n");
163 	MSG(0, "  -V print the version number and exit\n");
164 	exit(1);
165 }
166 
is_digits(char * optarg)167 static int is_digits(char *optarg)
168 {
169 	unsigned int i;
170 
171 	for (i = 0; i < strlen(optarg); i++)
172 		if (!isdigit(optarg[i]))
173 			break;
174 	return i == strlen(optarg);
175 }
176 
error_out(char * prog)177 static void error_out(char *prog)
178 {
179 	if (!strcmp("fsck.f2fs", prog))
180 		fsck_usage();
181 	else if (!strcmp("dump.f2fs", prog))
182 		dump_usage();
183 	else if (!strcmp("defrag.f2fs", prog))
184 		defrag_usage();
185 	else if (!strcmp("resize.f2fs", prog))
186 		resize_usage();
187 	else if (!strcmp("sload.f2fs", prog))
188 		sload_usage();
189 	else if (!strcmp("f2fslabel", prog))
190 		label_usage();
191 	else
192 		MSG(0, "\nWrong program.\n");
193 }
194 
__add_fsck_options(void)195 static void __add_fsck_options(void)
196 {
197 	/* -a */
198 	c.auto_fix = 1;
199 }
200 
add_default_options(void)201 static void add_default_options(void)
202 {
203 	switch (c.defset) {
204 	case CONF_ANDROID:
205 		__add_fsck_options();
206 	}
207 	c.quota_fix = 1;
208 }
209 
f2fs_parse_options(int argc,char * argv[])210 void f2fs_parse_options(int argc, char *argv[])
211 {
212 	int option = 0;
213 	char *prog = basename(argv[0]);
214 	int err = NOERROR;
215 #ifdef WITH_ANDROID
216 	int i;
217 
218 	/* Allow prog names (e.g, sload_f2fs, fsck_f2fs, etc) */
219 	for (i = 0; i < strlen(prog); i++) {
220 		if (prog[i] == '_')
221 			prog[i] = '.';
222 	}
223 #endif
224 	if (argc < 2) {
225 		MSG(0, "\tError: Device not specified\n");
226 		error_out(prog);
227 	}
228 
229 	if (!strcmp("fsck.f2fs", prog)) {
230 		const char *option_string = ":aC:c:m:Md:fg:lO:p:q:StyV";
231 		int opt = 0, val;
232 		char *token;
233 		struct option long_opt[] = {
234 			{"dry-run", no_argument, 0, 1},
235 			{"no-kernel-check", no_argument, 0, 2},
236 			{"kernel-check", no_argument, 0, 3},
237 			{"debug-cache", no_argument, 0, 4},
238 			{0, 0, 0, 0}
239 		};
240 
241 		c.func = FSCK;
242 		c.cache_config.max_hash_collision = 16;
243 		c.cache_config.dbg_en = false;
244 		while ((option = getopt_long(argc, argv, option_string,
245 						long_opt, &opt)) != EOF) {
246 			switch (option) {
247 			case 1:
248 				c.dry_run = 1;
249 				MSG(0, "Info: Dry run\n");
250 				break;
251 			case 2:
252 				c.no_kernel_check = 1;
253 				MSG(0, "Info: No Kernel Check\n");
254 				break;
255 			case 3:
256 				c.no_kernel_check = 0;
257 				MSG(0, "Info: Do Kernel Check\n");
258 				break;
259 			case 4:
260 				c.cache_config.dbg_en = true;
261 				break;
262 			case 'a':
263 				c.auto_fix = 1;
264 				MSG(0, "Info: Fix the reported corruption.\n");
265 				break;
266 			case 'c':
267 				c.cache_config.num_cache_entry = atoi(optarg);
268 				break;
269 			case 'm':
270 				c.cache_config.max_hash_collision =
271 						atoi(optarg);
272 				break;
273 			case 'g':
274 				if (!strcmp(optarg, "android"))
275 					c.defset = CONF_ANDROID;
276 				break;
277 			case 'l':
278 				c.layout = 1;
279 				break;
280 			case 'M':
281 				c.show_file_map = 1;
282 				break;
283 			case 'O':
284 				if (parse_feature(feature_table, optarg))
285 					fsck_usage();
286 				break;
287 			case 'p':
288 				/* preen mode has different levels:
289 				 *  0: default level, the same as -a
290 				 *  1: check meta
291 				 *  2: same as 0, but will skip some
292 				 *     check for old kernel
293 				 */
294 				if (optarg[0] == '-' || !is_digits(optarg) ||
295 							optind == argc) {
296 					MSG(0, "Info: Use default preen mode\n");
297 					c.preen_mode = PREEN_MODE_0;
298 					c.auto_fix = 1;
299 					optind--;
300 					break;
301 				}
302 				c.preen_mode = atoi(optarg);
303 				if (c.preen_mode < 0)
304 					c.preen_mode = PREEN_MODE_0;
305 				else if (c.preen_mode >= PREEN_MODE_MAX)
306 					c.preen_mode = PREEN_MODE_MAX - 1;
307 				if (c.preen_mode == PREEN_MODE_0 ||
308 					c.preen_mode == PREEN_MODE_2)
309 					c.auto_fix = 1;
310 				MSG(0, "Info: Fix the reported corruption in "
311 					"preen mode %d\n", c.preen_mode);
312 				break;
313 			case 'd':
314 				if (optarg[0] == '-') {
315 					err = ENEED_ARG;
316 					break;
317 				} else if (!is_digits(optarg)) {
318 					err = EWRONG_OPT;
319 					break;
320 				}
321 				c.dbg_lv = atoi(optarg);
322 				MSG(0, "Info: Debug level = %d\n", c.dbg_lv);
323 				break;
324 			case 'f':
325 			case 'y':
326 				c.fix_on = 1;
327 				c.force = 1;
328 				MSG(0, "Info: Force to fix corruption\n");
329 				break;
330 			case 'q':
331 				c.preserve_limits = atoi(optarg);
332 				MSG(0, "Info: Preserve quota limits = %d\n",
333 					c.preserve_limits);
334 				break;
335 			case 'S':
336 				c.sparse_mode = 1;
337 				break;
338 			case 't':
339 				c.show_dentry = 1;
340 				break;
341 			case ':':
342 				if (optopt == 'p') {
343 					MSG(0, "Info: Use default preen mode\n");
344 					c.preen_mode = PREEN_MODE_0;
345 					c.auto_fix = 1;
346 				} else {
347 					option = optopt;
348 					err = ENEED_ARG;
349 					break;
350 				}
351 				break;
352 			case 'C':
353 				token = strtok(optarg, ":");
354 				val = f2fs_str2encoding(token);
355 				if (val < 0) {
356 					MSG(0, "\tError: Unknown encoding %s\n", token);
357 					fsck_usage();
358 				}
359 				c.s_encoding = val;
360 				token = strtok(NULL, "");
361 				val = f2fs_str2encoding_flags(&token, &c.s_encoding_flags);
362 				if (val) {
363 					MSG(0, "\tError: Unknown flag %s\n", token);
364 					fsck_usage();
365 				}
366 				c.feature |= cpu_to_le32(F2FS_FEATURE_CASEFOLD);
367 				break;
368 			case 'V':
369 				show_version(prog);
370 				exit(0);
371 			case '?':
372 				option = optopt;
373 				fallthrough;
374 			default:
375 				err = EUNKNOWN_OPT;
376 				break;
377 			}
378 			if (err != NOERROR)
379 				break;
380 		}
381 	} else if (!strcmp("dump.f2fs", prog)) {
382 #ifdef WITH_DUMP
383 		const char *option_string = "d:i:n:Ms:Sa:b:V";
384 		static struct dump_option dump_opt = {
385 			.nid = 0,	/* default root ino */
386 			.start_nat = -1,
387 			.end_nat = -1,
388 			.start_sit = -1,
389 			.end_sit = -1,
390 			.start_ssa = -1,
391 			.end_ssa = -1,
392 			.blk_addr = -1,
393 		};
394 
395 		c.func = DUMP;
396 		while ((option = getopt(argc, argv, option_string)) != EOF) {
397 			int ret = 0;
398 
399 			switch (option) {
400 			case 'd':
401 				if (!is_digits(optarg)) {
402 					err = EWRONG_OPT;
403 					break;
404 				}
405 				c.dbg_lv = atoi(optarg);
406 				MSG(0, "Info: Debug level = %d\n",
407 							c.dbg_lv);
408 				break;
409 			case 'g':
410 				if (!strcmp(optarg, "android")) {
411 					c.defset = CONF_ANDROID;
412 					MSG(0, "Info: Set conf for android\n");
413 					break;
414 				}
415 				err = EWRONG_OPT;
416 				break;
417 			case 'i':
418 				if (strncmp(optarg, "0x", 2))
419 					ret = sscanf(optarg, "%d",
420 							&dump_opt.nid);
421 				else
422 					ret = sscanf(optarg, "%x",
423 							&dump_opt.nid);
424 				break;
425 			case 'n':
426 				ret = sscanf(optarg, "%d~%d",
427 							&dump_opt.start_nat,
428 							&dump_opt.end_nat);
429 				break;
430 			case 'M':
431 				c.show_file_map = 1;
432 				break;
433 			case 's':
434 				ret = sscanf(optarg, "%d~%d",
435 							&dump_opt.start_sit,
436 							&dump_opt.end_sit);
437 				break;
438 			case 'S':
439 				c.sparse_mode = 1;
440 				break;
441 			case 'a':
442 				ret = sscanf(optarg, "%d~%d",
443 							&dump_opt.start_ssa,
444 							&dump_opt.end_ssa);
445 				break;
446 			case 'b':
447 				if (strncmp(optarg, "0x", 2))
448 					ret = sscanf(optarg, "%d",
449 							&dump_opt.blk_addr);
450 				else
451 					ret = sscanf(optarg, "%x",
452 							&dump_opt.blk_addr);
453 				break;
454 			case 'V':
455 				show_version(prog);
456 				exit(0);
457 			default:
458 				err = EUNKNOWN_OPT;
459 				break;
460 			}
461 			ASSERT(ret >= 0);
462 			if (err != NOERROR)
463 				break;
464 		}
465 
466 		c.private = &dump_opt;
467 #endif
468 	} else if (!strcmp("defrag.f2fs", prog)) {
469 #ifdef WITH_DEFRAG
470 		const char *option_string = "d:s:Sl:t:iV";
471 
472 		c.func = DEFRAG;
473 		while ((option = getopt(argc, argv, option_string)) != EOF) {
474 			int ret = 0;
475 
476 			switch (option) {
477 			case 'd':
478 				if (!is_digits(optarg)) {
479 					err = EWRONG_OPT;
480 					break;
481 				}
482 				c.dbg_lv = atoi(optarg);
483 				MSG(0, "Info: Debug level = %d\n",
484 							c.dbg_lv);
485 				break;
486 			case 's':
487 				if (strncmp(optarg, "0x", 2))
488 					ret = sscanf(optarg, "%"PRIu64"",
489 							&c.defrag_start);
490 				else
491 					ret = sscanf(optarg, "%"PRIx64"",
492 							&c.defrag_start);
493 				break;
494 			case 'S':
495 				c.sparse_mode = 1;
496 				break;
497 			case 'l':
498 				if (strncmp(optarg, "0x", 2))
499 					ret = sscanf(optarg, "%"PRIu64"",
500 							&c.defrag_len);
501 				else
502 					ret = sscanf(optarg, "%"PRIx64"",
503 							&c.defrag_len);
504 				break;
505 			case 't':
506 				if (strncmp(optarg, "0x", 2))
507 					ret = sscanf(optarg, "%"PRIu64"",
508 							&c.defrag_target);
509 				else
510 					ret = sscanf(optarg, "%"PRIx64"",
511 							&c.defrag_target);
512 				break;
513 			case 'i':
514 				c.defrag_shrink = 1;
515 				break;
516 			case 'V':
517 				show_version(prog);
518 				exit(0);
519 			default:
520 				err = EUNKNOWN_OPT;
521 				break;
522 			}
523 			ASSERT(ret >= 0);
524 			if (err != NOERROR)
525 				break;
526 		}
527 #endif
528 	} else if (!strcmp("resize.f2fs", prog)) {
529 #ifdef WITH_RESIZE
530 		const char *option_string = "d:fst:iV";
531 
532 		c.func = RESIZE;
533 		while ((option = getopt(argc, argv, option_string)) != EOF) {
534 			int ret = 0;
535 
536 			switch (option) {
537 			case 'd':
538 				if (!is_digits(optarg)) {
539 					err = EWRONG_OPT;
540 					break;
541 				}
542 				c.dbg_lv = atoi(optarg);
543 				MSG(0, "Info: Debug level = %d\n",
544 							c.dbg_lv);
545 				break;
546 			case 'f':
547 				c.force = 1;
548 				MSG(0, "Info: Force to resize\n");
549 				break;
550 			case 's':
551 				c.safe_resize = 1;
552 				break;
553 			case 't':
554 				if (strncmp(optarg, "0x", 2))
555 					ret = sscanf(optarg, "%"PRIu64"",
556 							&c.target_sectors);
557 				else
558 					ret = sscanf(optarg, "%"PRIx64"",
559 							&c.target_sectors);
560 				break;
561 			case 'i':
562 				c.large_nat_bitmap = 1;
563 				break;
564 			case 'V':
565 				show_version(prog);
566 				exit(0);
567 			default:
568 				err = EUNKNOWN_OPT;
569 				break;
570 			}
571 			ASSERT(ret >= 0);
572 			if (err != NOERROR)
573 				break;
574 		}
575 #endif
576 	} else if (!strcmp("sload.f2fs", prog)) {
577 #ifdef WITH_SLOAD
578 		const char *option_string = "cL:a:i:x:m:rC:d:f:p:s:St:T:VP";
579 #ifdef HAVE_LIBSELINUX
580 		int max_nr_opt = (int)sizeof(c.seopt_file) /
581 			sizeof(c.seopt_file[0]);
582 		char *token;
583 #endif
584 		char *p;
585 
586 		c.func = SLOAD;
587 		c.compress.cc.log_cluster_size = 2;
588 		c.compress.alg = COMPR_LZ4;
589 		c.compress.min_blocks = 1;
590 		c.compress.filter_ops = &ext_filter;
591 		while ((option = getopt(argc, argv, option_string)) != EOF) {
592 			unsigned int i;
593 			int val;
594 
595 			switch (option) {
596 			case 'c': /* compression support */
597 				c.compress.enabled = true;
598 				break;
599 			case 'L': /* compression: log of blocks-per-cluster */
600 				c.compress.required = true;
601 				val = atoi(optarg);
602 				if (val < MIN_COMPRESS_LOG_SIZE ||
603 						val > MAX_COMPRESS_LOG_SIZE) {
604 					MSG(0, "\tError: log of blocks per"
605 						" cluster must be in the range"
606 						" of %d .. %d.\n",
607 						MIN_COMPRESS_LOG_SIZE,
608 						MAX_COMPRESS_LOG_SIZE);
609 					error_out(prog);
610 				}
611 				c.compress.cc.log_cluster_size = val;
612 				break;
613 			case 'a': /* compression: choose algorithm */
614 				c.compress.required = true;
615 				c.compress.alg = MAX_COMPRESS_ALGS;
616 				for (i = 0; i < MAX_COMPRESS_ALGS; i++) {
617 					if (!strcmp(supported_comp_names[i],
618 								optarg)) {
619 						c.compress.alg = i;
620 						break;
621 					}
622 				}
623 				if (c.compress.alg == MAX_COMPRESS_ALGS) {
624 					MSG(0, "\tError: Unknown compression"
625 						" algorithm %s\n", optarg);
626 					error_out(prog);
627 				}
628 				break;
629 			case 'i': /* compress only these extensions */
630 				c.compress.required = true;
631 				if (c.compress.filter == COMPR_FILTER_ALLOW) {
632 					MSG(0, "\tError: could not mix option"
633 							" -i and -x\n");
634 					error_out(prog);
635 				}
636 				c.compress.filter = COMPR_FILTER_DENY;
637 				c.compress.filter_ops->add(optarg);
638 				break;
639 			case 'x': /* compress except for these extensions */
640 				c.compress.required = true;
641 				if (c.compress.filter == COMPR_FILTER_DENY) {
642 					MSG(0, "\tError: could not mix option"
643 							" -i and -x\n");
644 					error_out(prog);
645 				}
646 				c.compress.filter = COMPR_FILTER_ALLOW;
647 				c.compress.filter_ops->add(optarg);
648 				break;
649 			case 'm': /* minimum compressed blocks per cluster */
650 				c.compress.required = true;
651 				val = atoi(optarg);
652 				if (val <= 0) {
653 					MSG(0, "\tError: minimum compressed"
654 						" blocks per cluster must be"
655 						" positive.\n");
656 					error_out(prog);
657 				}
658 				c.compress.min_blocks = val;
659 				break;
660 			case 'r': /* for setting FI_COMPRESS_RELEASED */
661 				c.compress.required = true;
662 				c.compress.readonly = true;
663 				break;
664 			case 'C':
665 				c.fs_config_file = absolute_path(optarg);
666 				break;
667 			case 'd':
668 				if (!is_digits(optarg)) {
669 					err = EWRONG_OPT;
670 					break;
671 				}
672 				c.dbg_lv = atoi(optarg);
673 				MSG(0, "Info: Debug level = %d\n",
674 						c.dbg_lv);
675 				break;
676 			case 'f':
677 				c.from_dir = absolute_path(optarg);
678 				break;
679 			case 'p':
680 				c.target_out_dir = absolute_path(optarg);
681 				break;
682 			case 's':
683 #ifdef HAVE_LIBSELINUX
684 				token = strtok(optarg, ",");
685 				while (token) {
686 					if (c.nr_opt == max_nr_opt) {
687 						MSG(0, "\tError: Expected at most %d selinux opts\n",
688 										max_nr_opt);
689 						error_out(prog);
690 					}
691 					c.seopt_file[c.nr_opt].type =
692 								SELABEL_OPT_PATH;
693 					c.seopt_file[c.nr_opt].value =
694 								absolute_path(token);
695 					c.nr_opt++;
696 					token = strtok(NULL, ",");
697 				}
698 #else
699 				MSG(0, "Info: Not support selinux opts\n");
700 #endif
701 				break;
702 			case 'S':
703 				c.sparse_mode = 1;
704 				break;
705 			case 't':
706 				c.mount_point = (char *)optarg;
707 				break;
708 			case 'T':
709 				c.fixed_time = strtoul(optarg, &p, 0);
710 				break;
711 			case 'V':
712 				show_version(prog);
713 				exit(0);
714 			case 'P':
715 				c.preserve_perms = 1;
716 				break;
717 			default:
718 				err = EUNKNOWN_OPT;
719 				break;
720 			}
721 			if (err != NOERROR)
722 				break;
723 		}
724 		if (c.compress.required && !c.compress.enabled) {
725 			MSG(0, "\tError: compression sub-options are used"
726 				" without the compression enable (-c) option\n"
727 			);
728 			error_out(prog);
729 		}
730 		if (err == NOERROR && c.compress.enabled) {
731 			c.compress.cc.cluster_size = 1
732 				<< c.compress.cc.log_cluster_size;
733 			if (c.compress.filter == COMPR_FILTER_UNASSIGNED)
734 				c.compress.filter = COMPR_FILTER_ALLOW;
735 			if (c.compress.min_blocks >=
736 					c.compress.cc.cluster_size) {
737 				MSG(0, "\tError: minimum reduced blocks by"
738 					" compression per cluster must be at"
739 					" most one less than blocks per"
740 					" cluster, i.e. %d\n",
741 					c.compress.cc.cluster_size - 1);
742 				error_out(prog);
743 			}
744 		}
745 #endif /* WITH_SLOAD */
746 	} else if (!strcmp("f2fslabel", prog)) {
747 #ifdef WITH_LABEL
748 		const char *option_string = "V";
749 
750 		c.func = LABEL;
751 		while ((option = getopt(argc, argv, option_string)) != EOF) {
752 			switch (option) {
753 			case 'V':
754 				show_version(prog);
755 				exit(0);
756 			default:
757 				err = EUNKNOWN_OPT;
758 				break;
759 			}
760 			if (err != NOERROR)
761 				break;
762 		}
763 
764 		if (argc > (optind + 2)) { /* unknown argument(s) is(are) passed */
765 			optind += 2;
766 			err = EUNKNOWN_ARG;
767 		} else if (argc == (optind + 2)) { /* change label */
768 			c.vol_label = argv[optind + 1];
769 			argc--;
770 		} else { /* print label */
771 			/*
772 			 * Since vol_label was initialized as "", in order to
773 			 * distinguish between clear label and print, set
774 			 * vol_label as NULL for print case
775 			 */
776 			c.vol_label = NULL;
777 		}
778 #endif /* WITH_LABEL */
779 	}
780 
781 	if (err == NOERROR) {
782 		add_default_options();
783 
784 		if (optind >= argc) {
785 			MSG(0, "\tError: Device not specified\n");
786 			error_out(prog);
787 		}
788 
789 		c.devices[0].path = strdup(argv[optind]);
790 		if (argc > (optind + 1)) {
791 			c.dbg_lv = 0;
792 			err = EUNKNOWN_ARG;
793 		}
794 		if (err == NOERROR)
795 			return;
796 	}
797 
798 	/* print out error */
799 	switch (err) {
800 	case EWRONG_OPT:
801 		MSG(0, "\tError: Wrong option -%c %s\n", option, optarg);
802 		break;
803 	case ENEED_ARG:
804 		MSG(0, "\tError: Need argument for -%c\n", option);
805 		break;
806 	case EUNKNOWN_OPT:
807 		MSG(0, "\tError: Unknown option %c\n", option);
808 		break;
809 	case EUNKNOWN_ARG:
810 		MSG(0, "\tError: Unknown argument %s\n", argv[optind]);
811 		break;
812 	}
813 	error_out(prog);
814 }
815 
do_fsck(struct f2fs_sb_info * sbi)816 static int do_fsck(struct f2fs_sb_info *sbi)
817 {
818 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
819 	u32 flag = le32_to_cpu(ckpt->ckpt_flags);
820 	u32 blk_cnt;
821 	struct f2fs_compr_blk_cnt cbc;
822 	errcode_t ret;
823 
824 	fsck_init(sbi);
825 
826 	print_cp_state(flag);
827 
828 	fsck_chk_and_fix_write_pointers(sbi);
829 
830 	fsck_chk_curseg_info(sbi);
831 
832 	if (!c.fix_on && !c.bug_on) {
833 		switch (c.preen_mode) {
834 		case PREEN_MODE_1:
835 			if (fsck_chk_meta(sbi)) {
836 				MSG(0, "[FSCK] F2FS metadata   [Fail]");
837 				MSG(0, "\tError: meta does not match, "
838 					"force check all\n");
839 			} else {
840 				MSG(0, "[FSCK] F2FS metadata   [Ok..]");
841 				fsck_free(sbi);
842 				return FSCK_SUCCESS;
843 			}
844 
845 			if (!c.ro)
846 				c.fix_on = 1;
847 			break;
848 		}
849 	} else if (c.preen_mode) {
850 		/*
851 		 * we can hit this in 3 situations:
852 		 *  1. fsck -f, fix_on has already been set to 1 when
853 		 *     parsing options;
854 		 *  2. fsck -a && CP_FSCK_FLAG is set, fix_on has already
855 		 *     been set to 1 when checking CP_FSCK_FLAG;
856 		 *  3. fsck -p 1 && error is detected, then bug_on is set,
857 		 *     we set fix_on = 1 here, so that fsck can fix errors
858 		 *     automatically
859 		*/
860 		c.fix_on = 1;
861 	}
862 
863 	fsck_chk_checkpoint(sbi);
864 
865 	fsck_chk_quota_node(sbi);
866 
867 	/* Traverse all block recursively from root inode */
868 	blk_cnt = 1;
869 	cbc.cnt = 0;
870 	cbc.cheader_pgofs = CHEADER_PGOFS_NONE;
871 
872 	if (c.feature & cpu_to_le32(F2FS_FEATURE_QUOTA_INO)) {
873 		ret = quota_init_context(sbi);
874 		if (ret) {
875 			ASSERT_MSG("quota_init_context failure: %d", ret);
876 			return FSCK_OPERATIONAL_ERROR;
877 		}
878 	}
879 	fsck_chk_orphan_node(sbi);
880 	fsck_chk_node_blk(sbi, NULL, sbi->root_ino_num,
881 			F2FS_FT_DIR, TYPE_INODE, &blk_cnt, &cbc, NULL);
882 	fsck_chk_quota_files(sbi);
883 
884 	ret = fsck_verify(sbi);
885 	fsck_free(sbi);
886 
887 	if (!c.bug_on)
888 		return FSCK_SUCCESS;
889 	if (!ret)
890 		return FSCK_ERROR_CORRECTED;
891 	return FSCK_ERRORS_LEFT_UNCORRECTED;
892 }
893 
894 #ifdef WITH_DUMP
do_dump(struct f2fs_sb_info * sbi)895 static void do_dump(struct f2fs_sb_info *sbi)
896 {
897 	struct dump_option *opt = (struct dump_option *)c.private;
898 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
899 	u32 flag = le32_to_cpu(ckpt->ckpt_flags);
900 
901 	if (opt->end_nat == -1)
902 		opt->end_nat = NM_I(sbi)->max_nid;
903 	if (opt->end_sit == -1)
904 		opt->end_sit = SM_I(sbi)->main_segments;
905 	if (opt->end_ssa == -1)
906 		opt->end_ssa = SM_I(sbi)->main_segments;
907 	if (opt->start_nat != -1)
908 		nat_dump(sbi, opt->start_nat, opt->end_nat);
909 	if (opt->start_sit != -1)
910 		sit_dump(sbi, opt->start_sit, opt->end_sit);
911 	if (opt->start_ssa != -1)
912 		ssa_dump(sbi, opt->start_ssa, opt->end_ssa);
913 	if (opt->blk_addr != -1)
914 		dump_info_from_blkaddr(sbi, opt->blk_addr);
915 	if (opt->nid)
916 		dump_node(sbi, opt->nid, 0);
917 
918 	print_cp_state(flag);
919 
920 }
921 #endif
922 
923 #ifdef WITH_DEFRAG
do_defrag(struct f2fs_sb_info * sbi)924 static int do_defrag(struct f2fs_sb_info *sbi)
925 {
926 	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
927 
928 	if (get_sb(feature) & cpu_to_le32(F2FS_FEATURE_RO)) {
929 		MSG(0, "Not support on readonly image.\n");
930 		return -1;
931 	}
932 
933 	if (c.defrag_start > get_sb(block_count))
934 		goto out_range;
935 	if (c.defrag_start < SM_I(sbi)->main_blkaddr)
936 		c.defrag_start = SM_I(sbi)->main_blkaddr;
937 
938 	if (c.defrag_len == 0)
939 		c.defrag_len = sbi->blocks_per_seg;
940 
941 	if (c.defrag_start + c.defrag_len > get_sb(block_count))
942 		c.defrag_len = get_sb(block_count) - c.defrag_start;
943 
944 	if (c.defrag_target == 0) {
945 		c.defrag_target = c.defrag_start - 1;
946 		if (!c.defrag_shrink)
947 			c.defrag_target += c.defrag_len + 1;
948 	}
949 
950 	if (c.defrag_target < SM_I(sbi)->main_blkaddr ||
951 			c.defrag_target > get_sb(block_count))
952 		goto out_range;
953 	if (c.defrag_target >= c.defrag_start &&
954 		c.defrag_target < c.defrag_start + c.defrag_len)
955 		goto out_range;
956 
957 	if (c.defrag_start > c.defrag_target)
958 		MSG(0, "Info: Move 0x%"PRIx64" <- [0x%"PRIx64"-0x%"PRIx64"]\n",
959 				c.defrag_target,
960 				c.defrag_start,
961 				c.defrag_start + c.defrag_len - 1);
962 	else
963 		MSG(0, "Info: Move [0x%"PRIx64"-0x%"PRIx64"] -> 0x%"PRIx64"\n",
964 				c.defrag_start,
965 				c.defrag_start + c.defrag_len - 1,
966 				c.defrag_target);
967 
968 	return f2fs_defragment(sbi, c.defrag_start, c.defrag_len,
969 			c.defrag_target, c.defrag_shrink);
970 out_range:
971 	ASSERT_MSG("Out-of-range [0x%"PRIx64" ~ 0x%"PRIx64"] to 0x%"PRIx64"",
972 				c.defrag_start,
973 				c.defrag_start + c.defrag_len - 1,
974 				c.defrag_target);
975 	return -1;
976 }
977 #endif
978 
979 #ifdef WITH_RESIZE
do_resize(struct f2fs_sb_info * sbi)980 static int do_resize(struct f2fs_sb_info *sbi)
981 {
982 	if (!c.target_sectors)
983 		c.target_sectors = c.total_sectors;
984 
985 	if (c.target_sectors > c.total_sectors) {
986 		ASSERT_MSG("Out-of-range Target=0x%"PRIx64" / 0x%"PRIx64"",
987 				c.target_sectors, c.total_sectors);
988 		return -1;
989 	}
990 
991 	return f2fs_resize(sbi);
992 }
993 #endif
994 
995 #ifdef WITH_SLOAD
init_compr(struct f2fs_sb_info * sbi)996 static int init_compr(struct f2fs_sb_info *sbi)
997 {
998 	if (!c.compress.enabled)
999 		return 0;
1000 
1001 	if (!(sbi->raw_super->feature
1002 			& cpu_to_le32(F2FS_FEATURE_COMPRESSION))) {
1003 		MSG(0, "Error: Compression (-c) was requested "
1004 			"but the file system is not created "
1005 			"with such feature.\n");
1006 		return -1;
1007 	}
1008 	if (!supported_comp_ops[c.compress.alg].init) {
1009 		MSG(0, "Error: The selected compression algorithm is not"
1010 				" supported\n");
1011 		return -1;
1012 	}
1013 	c.compress.ops = supported_comp_ops + c.compress.alg;
1014 	c.compress.ops->init(&c.compress.cc);
1015 	c.compress.ops->reset(&c.compress.cc);
1016 	c.compress.cc.rlen = c.compress.cc.cluster_size * F2FS_BLKSIZE;
1017 	return 0;
1018 }
1019 
do_sload(struct f2fs_sb_info * sbi)1020 static int do_sload(struct f2fs_sb_info *sbi)
1021 {
1022 	if (!c.from_dir) {
1023 		MSG(0, "Info: No source directory, but it's okay.\n");
1024 		return 0;
1025 	}
1026 	if (!c.mount_point)
1027 		c.mount_point = "/";
1028 
1029 	if (init_compr(sbi))
1030 		return -1;
1031 
1032 	return f2fs_sload(sbi);
1033 }
1034 #endif
1035 
1036 #ifdef WITH_LABEL
do_label(struct f2fs_sb_info * sbi)1037 static int do_label(struct f2fs_sb_info *sbi)
1038 {
1039 	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1040 
1041 	if (!c.vol_label) {
1042 		char label[MAX_VOLUME_NAME];
1043 
1044 		utf16_to_utf8(label, sb->volume_name,
1045 			      MAX_VOLUME_NAME, MAX_VOLUME_NAME);
1046 		MSG(0, "Info: volume label = %s\n", label);
1047 		return 0;
1048 	}
1049 
1050 	if (strlen(c.vol_label) > MAX_VOLUME_NAME) {
1051 		ERR_MSG("Label should not exceed %d characters\n", MAX_VOLUME_NAME);
1052 		return -1;
1053 	}
1054 
1055 	utf8_to_utf16(sb->volume_name, (const char *)c.vol_label,
1056 		      MAX_VOLUME_NAME, strlen(c.vol_label));
1057 
1058 	update_superblock(sb, SB_MASK_ALL);
1059 
1060 	MSG(0, "Info: volume label is changed to %s\n", c.vol_label);
1061 
1062 	return 0;
1063 }
1064 #endif
1065 
1066 #ifdef HAVE_MACH_TIME_H
get_boottime_ns()1067 static u64 get_boottime_ns()
1068 {
1069 	return mach_absolute_time();
1070 }
1071 #elif defined(HAVE_CLOCK_GETTIME) && defined(HAVE_CLOCK_BOOTTIME)
get_boottime_ns()1072 static u64 get_boottime_ns()
1073 {
1074 	struct timespec t;
1075 	t.tv_sec = t.tv_nsec = 0;
1076 	clock_gettime(CLOCK_BOOTTIME, &t);
1077 	return (u64)t.tv_sec * 1000000000LL + t.tv_nsec;
1078 }
1079 #else
get_boottime_ns()1080 static u64 get_boottime_ns()
1081 {
1082 	return 0;
1083 }
1084 #endif
1085 
main(int argc,char ** argv)1086 int main(int argc, char **argv)
1087 {
1088 	struct f2fs_sb_info *sbi;
1089 	int ret = 0, ret2;
1090 	u64 start = get_boottime_ns();
1091 
1092 	f2fs_init_configuration();
1093 
1094 	f2fs_parse_options(argc, argv);
1095 
1096 	if (c.func != DUMP && f2fs_devs_are_umounted() < 0) {
1097 		if (errno == EBUSY) {
1098 			ret = -1;
1099 			if (c.func == FSCK)
1100 				ret = FSCK_OPERATIONAL_ERROR;
1101 			goto quick_err;
1102 		}
1103 		if (!c.ro || c.func == DEFRAG) {
1104 			MSG(0, "\tError: Not available on mounted device!\n");
1105 			ret = -1;
1106 			if (c.func == FSCK)
1107 				ret = FSCK_OPERATIONAL_ERROR;
1108 			goto quick_err;
1109 		}
1110 
1111 		/* allow ro-mounted partition */
1112 		if (c.force) {
1113 			MSG(0, "Info: Force to check/repair FS on RO mounted device\n");
1114 		} else {
1115 			MSG(0, "Info: Check FS only on RO mounted device\n");
1116 			c.fix_on = 0;
1117 			c.auto_fix = 0;
1118 		}
1119 	}
1120 
1121 	/* Get device */
1122 	if (f2fs_get_device_info() < 0 || f2fs_get_f2fs_info() < 0) {
1123 		ret = -1;
1124 		if (c.func == FSCK)
1125 			ret = FSCK_OPERATIONAL_ERROR;
1126 		goto quick_err;
1127 	}
1128 
1129 fsck_again:
1130 	memset(&gfsck, 0, sizeof(gfsck));
1131 	gfsck.sbi.fsck = &gfsck;
1132 	sbi = &gfsck.sbi;
1133 
1134 	ret = f2fs_do_mount(sbi);
1135 	if (ret != 0) {
1136 		if (ret == 1) {
1137 			MSG(0, "Info: No error was reported\n");
1138 			ret = 0;
1139 		}
1140 		goto out_err;
1141 	}
1142 
1143 	switch (c.func) {
1144 	case FSCK:
1145 		ret = do_fsck(sbi);
1146 		break;
1147 #ifdef WITH_DUMP
1148 	case DUMP:
1149 		do_dump(sbi);
1150 		break;
1151 #endif
1152 #ifdef WITH_DEFRAG
1153 	case DEFRAG:
1154 		ret = do_defrag(sbi);
1155 		if (ret)
1156 			goto out_err;
1157 		break;
1158 #endif
1159 #ifdef WITH_RESIZE
1160 	case RESIZE:
1161 		if (do_resize(sbi))
1162 			goto out_err;
1163 		break;
1164 #endif
1165 #ifdef WITH_SLOAD
1166 	case SLOAD:
1167 		if (do_sload(sbi))
1168 			goto out_err;
1169 
1170 		ret = f2fs_sparse_initialize_meta(sbi);
1171 		if (ret < 0)
1172 			goto out_err;
1173 
1174 		f2fs_do_umount(sbi);
1175 
1176 		/* fsck to fix missing quota */
1177 		c.func = FSCK;
1178 		c.fix_on = 1;
1179 		goto fsck_again;
1180 #endif
1181 #ifdef WITH_LABEL
1182 	case LABEL:
1183 		if (do_label(sbi))
1184 			goto out_err;
1185 		break;
1186 #endif
1187 	default:
1188 		ERR_MSG("Wrong program name\n");
1189 		ASSERT(0);
1190 	}
1191 
1192 	f2fs_do_umount(sbi);
1193 
1194 	if (c.func == FSCK && c.bug_on) {
1195 		if (!c.ro && c.fix_on == 0 && c.auto_fix == 0 && !c.dry_run) {
1196 			char ans[255] = {0};
1197 retry:
1198 			printf("Do you want to fix this partition? [Y/N] ");
1199 			ret2 = scanf("%s", ans);
1200 			ASSERT(ret2 >= 0);
1201 			if (!strcasecmp(ans, "y"))
1202 				c.fix_on = 1;
1203 			else if (!strcasecmp(ans, "n"))
1204 				c.fix_on = 0;
1205 			else
1206 				goto retry;
1207 
1208 			if (c.fix_on)
1209 				goto fsck_again;
1210 		}
1211 	}
1212 	ret2 = f2fs_finalize_device();
1213 	if (ret2) {
1214 		if (c.func == FSCK)
1215 			return FSCK_OPERATIONAL_ERROR;
1216 		return ret2;
1217 	}
1218 
1219 	if (c.func == SLOAD)
1220 		c.compress.filter_ops->destroy();
1221 
1222 	if (!c.show_file_map)
1223 		printf("\nDone: %lf secs\n", (get_boottime_ns() - start) / 1000000000.0);
1224 	return ret;
1225 
1226 out_err:
1227 	if (sbi->ckpt)
1228 		free(sbi->ckpt);
1229 	if (sbi->raw_super)
1230 		free(sbi->raw_super);
1231 quick_err:
1232 	f2fs_release_sparse_resource();
1233 	return ret;
1234 }
1235