Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * blockdev implementation for busybox |
| 3 | * |
| 4 | * Copyright (C) 2010 Sergey Naumov <sknaumov@gmail.com> |
| 5 | * |
Denys Vlasenko | f38b912 | 2010-09-06 17:41:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 7 | */ |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 8 | //config:config BLOCKDEV |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 9 | //config: bool "blockdev (2.3 kb)" |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 10 | //config: default y |
| 11 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 12 | //config: Performs some ioctls with block devices. |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 13 | |
Denys Vlasenko | 9f59849 | 2017-08-05 01:29:12 +0200 | [diff] [blame] | 14 | //applet:IF_BLOCKDEV(APPLET_NOEXEC(blockdev, blockdev, BB_DIR_SBIN, BB_SUID_DROP, blockdev)) |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 15 | |
| 16 | //kbuild:lib-$(CONFIG_BLOCKDEV) += blockdev.o |
| 17 | |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 18 | //usage:#define blockdev_trivial_usage |
Denys Vlasenko | b4e6b41 | 2010-09-06 14:15:46 +0200 | [diff] [blame] | 19 | //usage: "OPTION BLOCKDEV" |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 20 | //usage:#define blockdev_full_usage "\n\n" |
Denys Vlasenko | 6642676 | 2011-06-05 03:58:28 +0200 | [diff] [blame] | 21 | //usage: " --setro Set ro" |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 22 | //usage: "\n --setrw Set rw" |
| 23 | //usage: "\n --getro Get ro" |
| 24 | //usage: "\n --getss Get sector size" |
| 25 | //usage: "\n --getbsz Get block size" |
| 26 | //usage: "\n --setbsz BYTES Set block size" |
Dan Fandrich | f303bdd | 2011-02-20 04:15:43 +0100 | [diff] [blame] | 27 | //usage: "\n --getsz Get device size in 512-byte sectors" |
Denys Vlasenko | 46d315a | 2021-06-16 17:00:56 +0200 | [diff] [blame] | 28 | ///////: "\n --getsize Get device size in sectors (deprecated)" |
| 29 | ///////^^^^^ supported, but not shown in help ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 30 | //usage: "\n --getsize64 Get device size in bytes" |
Denys Vlasenko | 46d315a | 2021-06-16 17:00:56 +0200 | [diff] [blame] | 31 | //usage: "\n --getra Get readahead in 512-byte sectors" |
| 32 | //usage: "\n --setra SECTORS Set readahead" |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 33 | //usage: "\n --flushbufs Flush buffers" |
| 34 | //usage: "\n --rereadpt Reread partition table" |
Denys Vlasenko | 023ae75 | 2019-06-08 08:57:11 +0200 | [diff] [blame] | 35 | // util-linux 2.31 also has: |
| 36 | // --getdiscardzeroes BLKDISCARDZEROES Get discard zeroes support status |
| 37 | // --getpbsz BLKPBSZGET Get physical block (sector) size |
| 38 | // --getiomin BLKIOMIN Get minimum I/O size |
| 39 | // --getioopt BLKIOOPT Get optimal I/O size |
| 40 | // --getalignoff BLKALIGNOFF Get alignment offset in bytes |
| 41 | // --getmaxsect BLKSECTGET Get max sectors per request |
| 42 | // --setra SECTORS BLKRASET Set readahead |
| 43 | // --getra BLKRAGET Get readahead |
| 44 | // --setfra SECTORS BLKFRASET Set filesystem readahead |
| 45 | // --getfra BLKFRAGET Get filesystem readahead |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 46 | |
| 47 | #include "libbb.h" |
| 48 | #include <linux/fs.h> |
| 49 | |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 50 | /* Takes less space is separate arrays than one array of struct */ |
| 51 | static const char bdcmd_names[] ALIGN1 = |
| 52 | "setro" "\0" |
| 53 | #define CMD_SETRO 0 |
| 54 | "setrw" "\0" |
| 55 | "getro" "\0" |
| 56 | "getss" "\0" |
| 57 | "getbsz" "\0" |
| 58 | "setbsz" "\0" |
| 59 | #define CMD_SETBSZ 5 |
| 60 | "getsz" "\0" |
| 61 | "getsize" "\0" |
| 62 | "getsize64" "\0" |
Denys Vlasenko | 46d315a | 2021-06-16 17:00:56 +0200 | [diff] [blame] | 63 | "getra" "\0" |
| 64 | "setra" "\0" |
| 65 | #define CMD_SETRA 10 |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 66 | "flushbufs" "\0" |
| 67 | "rereadpt" "\0" |
| 68 | ; |
Denys Vlasenko | 6cc4962 | 2020-11-30 14:58:02 +0100 | [diff] [blame] | 69 | static const uint32_t bdcmd_ioctl[] ALIGN4 = { |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 70 | BLKROSET, //setro |
| 71 | BLKROSET, //setrw |
| 72 | BLKROGET, //getro |
| 73 | BLKSSZGET, //getss |
| 74 | BLKBSZGET, //getbsz |
| 75 | BLKBSZSET, //setbsz |
| 76 | BLKGETSIZE64, //getsz |
| 77 | BLKGETSIZE, //getsize |
| 78 | BLKGETSIZE64, //getsize64 |
Denys Vlasenko | 46d315a | 2021-06-16 17:00:56 +0200 | [diff] [blame] | 79 | BLKRAGET, //getra |
| 80 | BLKRASET, //setra |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 81 | BLKFLSBUF, //flushbufs |
| 82 | BLKRRPART, //rereadpt |
| 83 | }; |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 84 | enum { |
| 85 | ARG_NONE = 0, |
| 86 | ARG_INT = 1, |
| 87 | ARG_ULONG = 2, |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 88 | /* Yes, BLKGETSIZE64 takes pointer to uint64_t, not ullong! */ |
| 89 | ARG_U64 = 3, |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 90 | ARG_MASK = 3, |
| 91 | |
| 92 | FL_USRARG = 4, /* argument is provided by user */ |
| 93 | FL_NORESULT = 8, |
Dan Fandrich | f303bdd | 2011-02-20 04:15:43 +0100 | [diff] [blame] | 94 | FL_SCALE512 = 16, |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 95 | }; |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 96 | static const uint8_t bdcmd_flags[] ALIGN1 = { |
| 97 | ARG_INT + FL_NORESULT, //setro |
| 98 | ARG_INT + FL_NORESULT, //setrw |
| 99 | ARG_INT, //getro |
| 100 | ARG_INT, //getss |
| 101 | ARG_INT, //getbsz |
| 102 | ARG_INT + FL_NORESULT + FL_USRARG, //setbsz |
| 103 | ARG_U64 + FL_SCALE512, //getsz |
| 104 | ARG_ULONG, //getsize |
| 105 | ARG_U64, //getsize64 |
Denys Vlasenko | 46d315a | 2021-06-16 17:00:56 +0200 | [diff] [blame] | 106 | ARG_ULONG, //getra |
| 107 | ARG_ULONG + FL_NORESULT, //setra |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 108 | ARG_NONE + FL_NORESULT, //flushbufs |
| 109 | ARG_NONE + FL_NORESULT, //rereadpt |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 110 | }; |
| 111 | |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 112 | static unsigned find_cmd(const char *s) |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 113 | { |
Denys Vlasenko | 264bdad | 2010-09-06 15:34:15 +0200 | [diff] [blame] | 114 | if (s[0] == '-' && s[1] == '-') { |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 115 | int n = index_in_strings(bdcmd_names, s + 2); |
| 116 | if (n >= 0) |
| 117 | return n; |
Denys Vlasenko | 264bdad | 2010-09-06 15:34:15 +0200 | [diff] [blame] | 118 | } |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 119 | bb_show_usage(); |
| 120 | } |
| 121 | |
| 122 | int blockdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Dan Fandrich | f303bdd | 2011-02-20 04:15:43 +0100 | [diff] [blame] | 123 | int blockdev_main(int argc UNUSED_PARAM, char **argv) |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 124 | { |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 125 | unsigned bdcmd; |
| 126 | unsigned flags; |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 127 | int fd; |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 128 | uint64_t u64; |
| 129 | union { |
| 130 | int i; |
| 131 | unsigned long lu; |
| 132 | uint64_t u64; |
| 133 | } ioctl_val_on_stack; |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 134 | |
Dan Fandrich | f303bdd | 2011-02-20 04:15:43 +0100 | [diff] [blame] | 135 | argv++; |
| 136 | if (!argv[0] || !argv[1]) /* must have at least 2 args */ |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 137 | bb_show_usage(); |
| 138 | |
Dan Fandrich | f303bdd | 2011-02-20 04:15:43 +0100 | [diff] [blame] | 139 | bdcmd = find_cmd(*argv); |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 140 | /* setrw translates to BLKROSET(0), most other ioctls don't care... */ |
| 141 | /* ...setro will do BLKROSET(1) */ |
| 142 | u64 = (bdcmd == CMD_SETRO); |
Denys Vlasenko | 46d315a | 2021-06-16 17:00:56 +0200 | [diff] [blame] | 143 | if (bdcmd == CMD_SETBSZ || bdcmd == CMD_SETRA) { |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 144 | /* ...setbsz is BLKBSZSET(bytes) */ |
Denys Vlasenko | 46d315a | 2021-06-16 17:00:56 +0200 | [diff] [blame] | 145 | /* ...setra is BLKRASET(512 bytes) */ |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 146 | u64 = xatoi_positive(*++argv); |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 147 | } |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 148 | |
Dan Fandrich | f303bdd | 2011-02-20 04:15:43 +0100 | [diff] [blame] | 149 | argv++; |
| 150 | if (!argv[0] || argv[1]) |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 151 | bb_show_usage(); |
Dan Fandrich | f303bdd | 2011-02-20 04:15:43 +0100 | [diff] [blame] | 152 | fd = xopen(argv[0], O_RDONLY); |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 153 | |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 154 | ioctl_val_on_stack.u64 = u64; |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 155 | flags = bdcmd_flags[bdcmd]; |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 156 | #if BB_BIG_ENDIAN |
| 157 | /* Store data properly wrt data size. |
| 158 | * (1) It's no-op for little-endian. |
Denys Vlasenko | 46d315a | 2021-06-16 17:00:56 +0200 | [diff] [blame] | 159 | * (2) it's no-op for 0 and -1. |
| 160 | * --setro uses arg != 0 and != -1, and it is ARG_INT |
| 161 | * --setbsz USER_VAL is also ARG_INT |
| 162 | * --setra USER_VAL is ARG_ULONG, but it is passed by value, |
| 163 | * not reference (see below in ioctl call). |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 164 | * Thus, we don't need to handle ARG_ULONG. |
| 165 | */ |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 166 | switch (flags & ARG_MASK) { |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 167 | case ARG_INT: |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 168 | ioctl_val_on_stack.i = (int)u64; |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 169 | break; |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 170 | # if 0 /* unused */ |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 171 | case ARG_ULONG: |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 172 | ioctl_val_on_stack.lu = (unsigned long)u64; |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 173 | break; |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 174 | # endif |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 175 | } |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 176 | #endif |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 177 | |
Denys Vlasenko | 46d315a | 2021-06-16 17:00:56 +0200 | [diff] [blame] | 178 | if (ioctl(fd, bdcmd_ioctl[bdcmd], |
| 179 | bdcmd == CMD_SETRA |
| 180 | ? (void*)(uintptr_t)u64 /* BLKRASET passes _value_, not pointer to it */ |
| 181 | : &ioctl_val_on_stack.u64 |
| 182 | ) == -1) |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 183 | bb_simple_perror_msg_and_die(*argv); |
| 184 | |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 185 | /* Fetch it into register(s) */ |
| 186 | u64 = ioctl_val_on_stack.u64; |
| 187 | |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 188 | if (flags & FL_SCALE512) |
Dan Fandrich | f303bdd | 2011-02-20 04:15:43 +0100 | [diff] [blame] | 189 | u64 >>= 9; |
| 190 | |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 191 | /* Zero- or one-extend the value if needed, then print */ |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 192 | switch (flags & (ARG_MASK+FL_NORESULT)) { |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 193 | case ARG_INT: |
| 194 | /* Smaller code when we use long long |
| 195 | * (gcc tail-merges printf call) |
| 196 | */ |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 197 | printf("%lld\n", (long long)(int)u64); |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 198 | break; |
| 199 | case ARG_ULONG: |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 200 | u64 = (unsigned long)u64; |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 201 | /* FALLTHROUGH */ |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 202 | case ARG_U64: |
| 203 | printf("%llu\n", (unsigned long long)u64); |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 204 | break; |
| 205 | } |
| 206 | |
| 207 | if (ENABLE_FEATURE_CLEAN_UP) |
| 208 | close(fd); |
| 209 | return EXIT_SUCCESS; |
| 210 | } |