]> git.ipfire.org Git - thirdparty/util-linux.git/blob - include/blkdev.h
Merge branch 'ci/meson-werror' of https://github.com/t-8ch/util-linux
[thirdparty/util-linux.git] / include / blkdev.h
1 /*
2 * No copyright is claimed. This code is in the public domain; do with
3 * it what you wish.
4 *
5 * Written by Karel Zak <kzak@redhat.com>
6 */
7 #ifndef BLKDEV_H
8 #define BLKDEV_H
9
10 #include <sys/types.h>
11 #include <sys/ioctl.h>
12 #ifdef HAVE_SYS_IOCCOM_H
13 # include <sys/ioccom.h> /* for _IO macro on e.g. Solaris */
14 #endif
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include <sys/stat.h>
18 #include <stdint.h>
19
20 #ifdef HAVE_SYS_MKDEV_H
21 # include <sys/mkdev.h> /* major and minor on Solaris */
22 #endif
23
24 #define DEFAULT_SECTOR_SIZE 512
25
26 #ifdef __linux__
27 /* very basic ioctls, should be available everywhere */
28 # ifndef BLKROSET
29 # define BLKROSET _IO(0x12,93) /* set device read-only (0 = read-write) */
30 # define BLKROGET _IO(0x12,94) /* get read-only status (0 = read_write) */
31 # define BLKRRPART _IO(0x12,95) /* re-read partition table */
32 # define BLKGETSIZE _IO(0x12,96) /* return device size /512 (long *arg) */
33 # define BLKFLSBUF _IO(0x12,97) /* flush buffer cache */
34 # define BLKRASET _IO(0x12,98) /* set read ahead for block device */
35 # define BLKRAGET _IO(0x12,99) /* get current read ahead setting */
36 # define BLKFRASET _IO(0x12,100) /* set filesystem (mm/filemap.c) read-ahead */
37 # define BLKFRAGET _IO(0x12,101) /* get filesystem (mm/filemap.c) read-ahead */
38 # define BLKSECTSET _IO(0x12,102) /* set max sectors per request (ll_rw_blk.c) */
39 # define BLKSECTGET _IO(0x12,103) /* get max sectors per request (ll_rw_blk.c) */
40 # define BLKSSZGET _IO(0x12,104) /* get block device sector size */
41
42 /* ioctls introduced in 2.2.16, removed in 2.5.58 */
43 # define BLKELVGET _IOR(0x12,106,size_t) /* elevator get */
44 # define BLKELVSET _IOW(0x12,107,size_t) /* elevator set */
45
46 # define BLKBSZGET _IOR(0x12,112,size_t)
47 # define BLKBSZSET _IOW(0x12,113,size_t)
48 # endif /* !BLKROSET */
49
50 # ifndef BLKGETSIZE64
51 # define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
52 # endif
53
54 /* block device topology ioctls, introduced in 2.6.32 (commit ac481c20) */
55 # ifndef BLKIOMIN
56 # define BLKIOMIN _IO(0x12,120)
57 # define BLKIOOPT _IO(0x12,121)
58 # define BLKALIGNOFF _IO(0x12,122)
59 # define BLKPBSZGET _IO(0x12,123)
60 # endif
61
62 /* discard zeroes support, introduced in 2.6.33 (commit 98262f27) */
63 # ifndef BLKDISCARDZEROES
64 # define BLKDISCARDZEROES _IO(0x12,124)
65 # endif
66
67 /* disk sequence number, introduced in 5.15 (commit 7957d93b) */
68 # ifndef BLKGETDISKSEQ
69 # define BLKGETDISKSEQ _IOR(0x12, 128, uint64_t)
70 # endif
71
72 /* filesystem freeze, introduced in 2.6.29 (commit fcccf502) */
73 # ifndef FIFREEZE
74 # define FIFREEZE _IOWR('X', 119, int) /* Freeze */
75 # define FITHAW _IOWR('X', 120, int) /* Thaw */
76 # endif
77
78 /* uniform CD-ROM information */
79 # ifndef CDROM_GET_CAPABILITY
80 # define CDROM_GET_CAPABILITY 0x5331
81 # endif
82
83 #endif /* __linux */
84
85
86 #ifdef APPLE_DARWIN
87 # define BLKGETSIZE DKIOCGETBLOCKCOUNT32
88 #endif
89
90 #ifndef HDIO_GETGEO
91 # ifdef __linux__
92 # define HDIO_GETGEO 0x0301
93 # endif
94
95 struct hd_geometry {
96 unsigned char heads;
97 unsigned char sectors;
98 unsigned short cylinders; /* truncated */
99 unsigned long start;
100 };
101 #endif /* HDIO_GETGEO */
102
103
104 /* are we working with block device? */
105 int is_blkdev(int fd);
106
107 /* open block device or file */
108 int open_blkdev_or_file(const struct stat *st, const char *name, const int oflag);
109
110 /* Determine size in bytes */
111 off_t blkdev_find_size (int fd);
112
113 /* get size in bytes */
114 int blkdev_get_size(int fd, unsigned long long *bytes);
115
116 /* get 512-byte sector count */
117 int blkdev_get_sectors(int fd, unsigned long long *sectors);
118
119 /* get hardware sector size */
120 int blkdev_get_sector_size(int fd, int *sector_size);
121
122 /* specifies whether or not the device is misaligned */
123 int blkdev_is_misaligned(int fd);
124
125 /* get physical block device size */
126 int blkdev_get_physector_size(int fd, int *sector_size);
127
128 /* is the device cdrom capable? */
129 int blkdev_is_cdrom(int fd);
130
131 /* get device's geometry - legacy */
132 int blkdev_get_geometry(int fd, unsigned int *h, unsigned int *s);
133
134 /* SCSI device types. Copied almost as-is from kernel header.
135 * http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/include/scsi/scsi.h */
136 #define SCSI_TYPE_DISK 0x00
137 #define SCSI_TYPE_TAPE 0x01
138 #define SCSI_TYPE_PRINTER 0x02
139 #define SCSI_TYPE_PROCESSOR 0x03 /* HP scanners use this */
140 #define SCSI_TYPE_WORM 0x04 /* Treated as ROM by our system */
141 #define SCSI_TYPE_ROM 0x05
142 #define SCSI_TYPE_SCANNER 0x06
143 #define SCSI_TYPE_MOD 0x07 /* Magneto-optical disk - treated as SCSI_TYPE_DISK */
144 #define SCSI_TYPE_MEDIUM_CHANGER 0x08
145 #define SCSI_TYPE_COMM 0x09 /* Communications device */
146 #define SCSI_TYPE_RAID 0x0c
147 #define SCSI_TYPE_ENCLOSURE 0x0d /* Enclosure Services Device */
148 #define SCSI_TYPE_RBC 0x0e
149 #define SCSI_TYPE_OSD 0x11
150 #define SCSI_TYPE_NO_LUN 0x7f
151
152 /* convert scsi type code to name */
153 const char *blkdev_scsi_type_to_name(int type);
154
155 int blkdev_lock(int fd, const char *devname, const char *lockmode);
156 #ifdef HAVE_LINUX_BLKZONED_H
157 struct blk_zone_report *blkdev_get_zonereport(int fd, uint64_t sector, uint32_t nzones);
158 #else
159 static inline struct blk_zone_report *blkdev_get_zonereport(int fd, uint64_t sector, uint32_t nzones)
160 {
161 return NULL;
162 }
163 #endif
164
165 #endif /* BLKDEV_H */