diff options
author | Malek Degachi <malek-degachi@laposte.net> | 2013-11-09 21:27:27 +0100 |
---|---|---|
committer | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2013-11-09 22:14:33 +0100 |
commit | 1d39e33d46f32862da2ec1c394401239515206b8 (patch) | |
tree | c90f67e5400ad40becda932e95b64eace13a092e | |
parent | 80f8cdf04ad0e8b37c20aed883b6cbfd6636dbc3 (diff) | |
download | busybox-w32-1d39e33d46f32862da2ec1c394401239515206b8.tar.gz busybox-w32-1d39e33d46f32862da2ec1c394401239515206b8.tar.bz2 busybox-w32-1d39e33d46f32862da2ec1c394401239515206b8.zip |
fstrim: New applet
fstrim applet is a port from util-linux.
"Trimming" your NAND/eMMC storage will restore the write performance
back to normal after having slow down issues on sequential write and
random write due to usage over time.
Good reading on subject:
http://forum.xda-developers.com/showthread.php?t=1971852
(with long options and CLEAN_UP turned on)
function old new delta
.rodata 148494 148791 +297
fstrim_main - 283 +283
fstrim_sfx - 128 +128
packed_usage 28826 28903 +77
applet_main 2760 2768 +8
applet_names 2343 2350 +7
applet_nameofs 690 692 +2
------------------------------------------------------------------------------
(add/remove: 3/0 grow/shrink: 5/0 up/down: 802/0) Total: 802 bytes
Signed-off-by: Malek Degachi <malek-degachi@laposte.net>
Cc: Eugene San (eugenesan) <eugenesan@gmail.com>
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
-rw-r--r-- | include/applets.src.h | 1 | ||||
-rw-r--r-- | util-linux/Config.src | 7 | ||||
-rw-r--r-- | util-linux/Kbuild.src | 1 | ||||
-rw-r--r-- | util-linux/fstrim.c | 111 |
4 files changed, 120 insertions, 0 deletions
diff --git a/include/applets.src.h b/include/applets.src.h index 3a47e15b9..5268200f5 100644 --- a/include/applets.src.h +++ b/include/applets.src.h | |||
@@ -167,6 +167,7 @@ IF_FSCK(APPLET(fsck, BB_DIR_SBIN, BB_SUID_DROP)) | |||
167 | //IF_E2FSCK(APPLET_ODDNAME(fsck.ext2, e2fsck, BB_DIR_SBIN, BB_SUID_DROP, fsck_ext2)) | 167 | //IF_E2FSCK(APPLET_ODDNAME(fsck.ext2, e2fsck, BB_DIR_SBIN, BB_SUID_DROP, fsck_ext2)) |
168 | //IF_E2FSCK(APPLET_ODDNAME(fsck.ext3, e2fsck, BB_DIR_SBIN, BB_SUID_DROP, fsck_ext3)) | 168 | //IF_E2FSCK(APPLET_ODDNAME(fsck.ext3, e2fsck, BB_DIR_SBIN, BB_SUID_DROP, fsck_ext3)) |
169 | IF_FSCK_MINIX(APPLET_ODDNAME(fsck.minix, fsck_minix, BB_DIR_SBIN, BB_SUID_DROP, fsck_minix)) | 169 | IF_FSCK_MINIX(APPLET_ODDNAME(fsck.minix, fsck_minix, BB_DIR_SBIN, BB_SUID_DROP, fsck_minix)) |
170 | IF_FSTRIM(APPLET(fstrim, BB_DIR_SBIN, BB_SUID_DROP)) | ||
170 | IF_FSYNC(APPLET_NOFORK(fsync, fsync, BB_DIR_BIN, BB_SUID_DROP, fsync)) | 171 | IF_FSYNC(APPLET_NOFORK(fsync, fsync, BB_DIR_BIN, BB_SUID_DROP, fsync)) |
171 | IF_FTPD(APPLET(ftpd, BB_DIR_USR_SBIN, BB_SUID_DROP)) | 172 | IF_FTPD(APPLET(ftpd, BB_DIR_USR_SBIN, BB_SUID_DROP)) |
172 | IF_FTPGET(APPLET_ODDNAME(ftpget, ftpgetput, BB_DIR_USR_BIN, BB_SUID_DROP, ftpget)) | 173 | IF_FTPGET(APPLET_ODDNAME(ftpget, ftpgetput, BB_DIR_USR_BIN, BB_SUID_DROP, ftpget)) |
diff --git a/util-linux/Config.src b/util-linux/Config.src index 5a8b0063b..ef7039720 100644 --- a/util-linux/Config.src +++ b/util-linux/Config.src | |||
@@ -246,6 +246,13 @@ config FSCK_MINIX | |||
246 | check for and attempt to repair any corruption that occurs to a minix | 246 | check for and attempt to repair any corruption that occurs to a minix |
247 | filesystem. | 247 | filesystem. |
248 | 248 | ||
249 | config FSTRIM | ||
250 | bool "fstrim" | ||
251 | default y | ||
252 | select PLATFORM_LINUX | ||
253 | help | ||
254 | Discard unused blocks on a mounted filesystem. | ||
255 | |||
249 | config MKFS_EXT2 | 256 | config MKFS_EXT2 |
250 | bool "mkfs_ext2" | 257 | bool "mkfs_ext2" |
251 | default y | 258 | default y |
diff --git a/util-linux/Kbuild.src b/util-linux/Kbuild.src index 468fc6bc1..429cf1100 100644 --- a/util-linux/Kbuild.src +++ b/util-linux/Kbuild.src | |||
@@ -18,6 +18,7 @@ lib-$(CONFIG_FINDFS) += findfs.o | |||
18 | lib-$(CONFIG_FLOCK) += flock.o | 18 | lib-$(CONFIG_FLOCK) += flock.o |
19 | lib-$(CONFIG_FREERAMDISK) += freeramdisk.o | 19 | lib-$(CONFIG_FREERAMDISK) += freeramdisk.o |
20 | lib-$(CONFIG_FSCK_MINIX) += fsck_minix.o | 20 | lib-$(CONFIG_FSCK_MINIX) += fsck_minix.o |
21 | lib-$(CONFIG_FSTRIM) += fstrim.o | ||
21 | lib-$(CONFIG_GETOPT) += getopt.o | 22 | lib-$(CONFIG_GETOPT) += getopt.o |
22 | lib-$(CONFIG_HEXDUMP) += hexdump.o | 23 | lib-$(CONFIG_HEXDUMP) += hexdump.o |
23 | lib-$(CONFIG_HWCLOCK) += hwclock.o | 24 | lib-$(CONFIG_HWCLOCK) += hwclock.o |
diff --git a/util-linux/fstrim.c b/util-linux/fstrim.c new file mode 100644 index 000000000..2fa457b5e --- /dev/null +++ b/util-linux/fstrim.c | |||
@@ -0,0 +1,111 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * fstrim.c - discard the part (or whole) of mounted filesystem. | ||
4 | * | ||
5 | * 03 March 2012 - Malek Degachi <malek-degachi@laposte.net> | ||
6 | * Adapted for busybox from util-linux-2.12a. | ||
7 | * | ||
8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. | ||
9 | */ | ||
10 | |||
11 | //usage:#define fstrim_trivial_usage | ||
12 | //usage: "[Options] <mountpoint>" | ||
13 | //usage:#define fstrim_full_usage "\n\n" | ||
14 | //usage: "Options:" | ||
15 | //usage: IF_LONG_OPTS( | ||
16 | //usage: "\n -o,--offset=offset offset in bytes to discard from" | ||
17 | //usage: "\n -l,--length=length length of bytes to discard from the offset" | ||
18 | //usage: "\n -m,--minimum=minimum minimum extent length to discard" | ||
19 | //usage: "\n -v,--verbose print number of discarded bytes" | ||
20 | //usage: ) | ||
21 | //usage: IF_NOT_LONG_OPTS( | ||
22 | //usage: "\n -o offset offset in bytes to discard from" | ||
23 | //usage: "\n -l length length of bytes to discard from the offset" | ||
24 | //usage: "\n -m minimum minimum extent length to discard" | ||
25 | //usage: "\n -v, print number of discarded bytes" | ||
26 | //usage: ) | ||
27 | |||
28 | #include "libbb.h" | ||
29 | #include <linux/fs.h> | ||
30 | |||
31 | #ifndef FITRIM | ||
32 | struct fstrim_range { | ||
33 | uint64_t start; | ||
34 | uint64_t len; | ||
35 | uint64_t minlen; | ||
36 | }; | ||
37 | #define FITRIM _IOWR('X', 121, struct fstrim_range) | ||
38 | #endif | ||
39 | |||
40 | static const struct suffix_mult fstrim_sfx[] = { | ||
41 | { "KiB", 1024 }, | ||
42 | { "kiB", 1024 }, | ||
43 | { "K", 1024 }, | ||
44 | { "k", 1024 }, | ||
45 | { "MiB", 1048576 }, | ||
46 | { "miB", 1048576 }, | ||
47 | { "M", 1048576 }, | ||
48 | { "m", 1048576 }, | ||
49 | { "GiB", 1073741824 }, | ||
50 | { "giB", 1073741824 }, | ||
51 | { "G", 1073741824 }, | ||
52 | { "g", 1073741824 }, | ||
53 | { "KB", 1000 }, | ||
54 | { "MB", 1000000 }, | ||
55 | { "GB", 1000000000 }, | ||
56 | { "", 0 } | ||
57 | }; | ||
58 | |||
59 | int fstrim_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
60 | int fstrim_main(int argc UNUSED_PARAM, char **argv) | ||
61 | { | ||
62 | struct fstrim_range range; | ||
63 | char *arg_o; | ||
64 | char *arg_l; | ||
65 | char *arg_m; | ||
66 | unsigned opts; | ||
67 | int fd; | ||
68 | |||
69 | enum { | ||
70 | OPT_o = (1 << 0), | ||
71 | OPT_l = (1 << 1), | ||
72 | OPT_m = (1 << 2), | ||
73 | OPT_v = (1 << 3), | ||
74 | }; | ||
75 | |||
76 | #if ENABLE_LONG_OPTS | ||
77 | static const char getopt_longopts[] ALIGN1 = | ||
78 | "offset\0" Required_argument "o" | ||
79 | "length\0" Required_argument "l" | ||
80 | "minimum\0" Required_argument "m" | ||
81 | "verbose\0" No_argument "v" | ||
82 | ; | ||
83 | applet_long_options = getopt_longopts; | ||
84 | #endif | ||
85 | |||
86 | opt_complementary = "=1"; /* exactly one non-option arg: the mountpoint */ | ||
87 | opts = getopt32(argv, "o:l:m:v", &arg_o, &arg_l, &arg_m); | ||
88 | |||
89 | memset(&range, 0, sizeof(range)); | ||
90 | range.len = ULLONG_MAX; | ||
91 | |||
92 | if (opts & OPT_o) | ||
93 | range.start = xatoull_sfx(arg_o, fstrim_sfx); | ||
94 | if (opts & OPT_l) | ||
95 | range.len = xatoull_sfx(arg_l, fstrim_sfx); | ||
96 | if (opts & OPT_m) | ||
97 | range.minlen = xatoull_sfx(arg_m, fstrim_sfx); | ||
98 | |||
99 | if (find_block_device(argv[optind])) { | ||
100 | fd = xopen_nonblocking(argv[optind]); | ||
101 | xioctl(fd, FITRIM, &range); | ||
102 | if (ENABLE_FEATURE_CLEAN_UP) | ||
103 | close(fd); | ||
104 | |||
105 | if (opts & OPT_v) | ||
106 | printf("%s: %llu bytes were trimmed\n", argv[optind], range.len); | ||
107 | } | ||
108 | |||
109 | return EXIT_SUCCESS; | ||
110 | } | ||
111 | |||