annotate toys/other/blockdev.c @ 1776:7bf68329eb3b draft default tip

Repository switched to git at https://github.com/landley/toybox
author Rob Landley <rob@landley.net>
date Thu, 09 Apr 2015 02:28:32 -0500
parents 8b7ddefcf28c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1434
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
1 /* blockdev.c -show/set blockdev information.
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
2 *
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
3 * Copyright 2014 Sameer Prakash Pradhan <sameer.p.pradhan@gmail.com>
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
4 *
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
5 * No Standard.
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
6
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
7 USE_BLOCKDEV(NEWTOY(blockdev, "<1>1(setro)(setrw)(getro)(getss)(getbsz)(setbsz)#<0(getsz)(getsize)(getsize64)(flushbufs)(rereadpt)",TOYFLAG_USR|TOYFLAG_BIN))
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
8
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
9 config BLOCKDEV
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
10 bool "blockdev"
1445
8b7ddefcf28c Promote blockdev to other.
Rob Landley <rob@landley.net>
parents: 1444
diff changeset
11 default y
1434
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
12 help
1444
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
13 usage: blockdev --OPTION... BLOCKDEV...
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
14
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
15 Call ioctl(s) on each listed block device
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
16
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
17 OPTIONs:
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
18 --setro Set read only
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
19 --setrw Set read write
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
20 --getro Get read only
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
21 --getss Get sector size
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
22 --getbsz Get block size
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
23 --setbsz BYTES Set block size
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
24 --getsz Get device size in 512-byte sectors
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
25 --getsize Get device size in sectors (deprecated)
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
26 --getsize64 Get device size in bytes
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
27 --flushbufs Flush buffers
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
28 --rereadpt Reread partition table
1434
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
29 */
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
30
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
31 #define FOR_blockdev
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
32 #include "toys.h"
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
33 #include <linux/fs.h>
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
34
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
35 GLOBALS(
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
36 long bsz;
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
37 )
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
38
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
39 void blockdev_main(void)
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
40 {
1444
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
41 int cmds[] = {BLKRRPART, BLKFLSBUF, BLKGETSIZE64, BLKGETSIZE, BLKGETSIZE64,
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
42 BLKBSZSET, BLKBSZGET, BLKSSZGET, BLKROGET, BLKROSET, BLKROSET};
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
43 char **ss;
1434
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
44 long long val = 0;
1444
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
45
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
46 if (!toys.optflags) {
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
47 toys.exithelp = 1;
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
48 error_exit("need --option");
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
49 }
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
50
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
51 for (ss = toys.optargs; *ss; ss++) {
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
52 int fd = xopen(*ss, O_RDONLY), i;
1434
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
53
1444
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
54 // Command line order discarded so perform multiple operations in flag order
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
55 for (i = 0; i < 32; i++) {
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
56 long flag = toys.optflags & (1<<i);
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
57
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
58 if (!flag) continue;
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
59
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
60 if (flag & FLAG_setbsz) val = TT.bsz;
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
61 else val = !!(flag & FLAG_setro);
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
62
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
63 xioctl(fd, cmds[i], &val);
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
64
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
65 flag &= FLAG_setbsz|FLAG_setro|FLAG_flushbufs|FLAG_rereadpt|FLAG_setrw;
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
66 if (!flag) printf("%lld\n", (toys.optflags & FLAG_getsz) ? val >> 9: val);
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
67 }
07e74271655e Cleanup blockdev.
Rob Landley <rob@landley.net>
parents: 1434
diff changeset
68 xclose(fd);
1434
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
69 }
08a34594b2c6 A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
70 }