1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_GENHD_H
3 #define _LINUX_GENHD_H
4
5 /*
6 * genhd.h Copyright (C) 1992 Drew Eckhardt
7 * Generic hard disk header file by
8 * Drew Eckhardt
9 *
10 * <drew@colorado.edu>
11 */
12
13 #include <linux/types.h>
14 #include <linux/kdev_t.h>
15 #include <linux/uuid.h>
16 #include <linux/blk_types.h>
17 #include <linux/device.h>
18 #include <linux/xarray.h>
19 #include <linux/android_kabi.h>
20
21 extern const struct device_type disk_type;
22 extern struct device_type part_type;
23 extern struct class block_class;
24
25 #define DISK_MAX_PARTS 256
26 #define DISK_NAME_LEN 32
27
28 #define PARTITION_META_INFO_VOLNAMELTH 64
29 /*
30 * Enough for the string representation of any kind of UUID plus NULL.
31 * EFI UUID is 36 characters. MSDOS UUID is 11 characters.
32 */
33 #define PARTITION_META_INFO_UUIDLTH (UUID_STRING_LEN + 1)
34
35 struct partition_meta_info {
36 char uuid[PARTITION_META_INFO_UUIDLTH];
37 u8 volname[PARTITION_META_INFO_VOLNAMELTH];
38 };
39
40 /**
41 * DOC: genhd capability flags
42 *
43 * ``GENHD_FL_REMOVABLE`` (0x0001): indicates that the block device
44 * gives access to removable media.
45 * When set, the device remains present even when media is not
46 * inserted.
47 * Must not be set for devices which are removed entirely when the
48 * media is removed.
49 *
50 * ``GENHD_FL_CD`` (0x0008): the block device is a CD-ROM-style
51 * device.
52 * Affects responses to the ``CDROM_GET_CAPABILITY`` ioctl.
53 *
54 * ``GENHD_FL_SUPPRESS_PARTITION_INFO`` (0x0020): don't include
55 * partition information in ``/proc/partitions`` or in the output of
56 * printk_all_partitions().
57 * Used for the null block device and some MMC devices.
58 *
59 * ``GENHD_FL_EXT_DEVT`` (0x0040): the driver supports extended
60 * dynamic ``dev_t``, i.e. it wants extended device numbers
61 * (``BLOCK_EXT_MAJOR``).
62 * This affects the maximum number of partitions.
63 *
64 * ``GENHD_FL_NO_PART`` (0x0200): partition support is disabled.
65 * The kernel will not scan for partitions from add_disk, and users
66 * can't add partitions manually.
67 *
68 * ``GENHD_FL_HIDDEN`` (0x0400): the block device is hidden; it
69 * doesn't produce events, doesn't appear in sysfs, and doesn't have
70 * an associated ``bdev``.
71 * Implies ``GENHD_FL_SUPPRESS_PARTITION_INFO`` and
72 * ``GENHD_FL_NO_PART``.
73 * Used for multipath devices.
74 */
75 #define GENHD_FL_REMOVABLE 0x0001
76 /* 2 is unused (used to be GENHD_FL_DRIVERFS) */
77 /* 4 is unused (used to be GENHD_FL_MEDIA_CHANGE_NOTIFY) */
78 #define GENHD_FL_CD 0x0008
79 #define GENHD_FL_SUPPRESS_PARTITION_INFO 0x0020
80 #define GENHD_FL_EXT_DEVT 0x0040
81 #define GENHD_FL_NO_PART 0x0200
82 #define GENHD_FL_HIDDEN 0x0400
83
84 enum {
85 DISK_EVENT_MEDIA_CHANGE = 1 << 0, /* media changed */
86 DISK_EVENT_EJECT_REQUEST = 1 << 1, /* eject requested */
87 };
88
89 enum {
90 /* Poll even if events_poll_msecs is unset */
91 DISK_EVENT_FLAG_POLL = 1 << 0,
92 /* Forward events to udev */
93 DISK_EVENT_FLAG_UEVENT = 1 << 1,
94 /* Block event polling when open for exclusive write */
95 DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE = 1 << 2,
96 };
97
98 struct disk_events;
99 struct badblocks;
100
101 struct blk_integrity {
102 const struct blk_integrity_profile *profile;
103 unsigned char flags;
104 unsigned char tuple_size;
105 unsigned char interval_exp;
106 unsigned char tag_size;
107
108 ANDROID_KABI_RESERVE(1);
109 ANDROID_KABI_RESERVE(2);
110 };
111
112 struct gendisk {
113 /* major, first_minor and minors are input parameters only,
114 * don't use directly. Use disk_devt() and disk_max_parts().
115 */
116 int major; /* major number of driver */
117 int first_minor;
118 int minors; /* maximum number of minors, =1 for
119 * disks that can't be partitioned. */
120
121 char disk_name[DISK_NAME_LEN]; /* name of major driver */
122
123 unsigned short events; /* supported events */
124 unsigned short event_flags; /* flags related to event processing */
125
126 struct xarray part_tbl;
127 struct block_device *part0;
128
129 const struct block_device_operations *fops;
130 struct request_queue *queue;
131 void *private_data;
132
133 int flags;
134 unsigned long state;
135 #define GD_NEED_PART_SCAN 0
136 #define GD_READ_ONLY 1
137 #define GD_DEAD 2
138 #define GD_NATIVE_CAPACITY 3
139
140 struct mutex open_mutex; /* open/close mutex */
141 unsigned open_partitions; /* number of open partitions */
142
143 struct backing_dev_info *bdi;
144 struct kobject *slave_dir;
145 #ifdef CONFIG_BLOCK_HOLDER_DEPRECATED
146 struct list_head slave_bdevs;
147 #endif
148 struct timer_rand_state *random;
149 atomic_t sync_io; /* RAID */
150 struct disk_events *ev;
151 #ifdef CONFIG_BLK_DEV_INTEGRITY
152 struct kobject integrity_kobj;
153 #endif /* CONFIG_BLK_DEV_INTEGRITY */
154 #if IS_ENABLED(CONFIG_CDROM)
155 struct cdrom_device_info *cdi;
156 #endif
157 int node_id;
158 struct badblocks *bb;
159 struct lockdep_map lockdep_map;
160 u64 diskseq;
161
162 ANDROID_KABI_RESERVE(1);
163 ANDROID_KABI_RESERVE(2);
164 ANDROID_KABI_RESERVE(3);
165 ANDROID_KABI_RESERVE(4);
166 };
167
disk_live(struct gendisk * disk)168 static inline bool disk_live(struct gendisk *disk)
169 {
170 return !inode_unhashed(disk->part0->bd_inode);
171 }
172
173 /*
174 * The gendisk is refcounted by the part0 block_device, and the bd_device
175 * therein is also used for device model presentation in sysfs.
176 */
177 #define dev_to_disk(device) \
178 (dev_to_bdev(device)->bd_disk)
179 #define disk_to_dev(disk) \
180 (&((disk)->part0->bd_device))
181
182 #if IS_REACHABLE(CONFIG_CDROM)
183 #define disk_to_cdi(disk) ((disk)->cdi)
184 #else
185 #define disk_to_cdi(disk) NULL
186 #endif
187
disk_max_parts(struct gendisk * disk)188 static inline int disk_max_parts(struct gendisk *disk)
189 {
190 if (disk->flags & GENHD_FL_EXT_DEVT)
191 return DISK_MAX_PARTS;
192 return disk->minors;
193 }
194
disk_part_scan_enabled(struct gendisk * disk)195 static inline bool disk_part_scan_enabled(struct gendisk *disk)
196 {
197 return disk_max_parts(disk) > 1 && !(disk->flags & GENHD_FL_NO_PART);
198 }
199
disk_devt(struct gendisk * disk)200 static inline dev_t disk_devt(struct gendisk *disk)
201 {
202 return MKDEV(disk->major, disk->first_minor);
203 }
204
205 void disk_uevent(struct gendisk *disk, enum kobject_action action);
206
207 /* block/genhd.c */
208 int device_add_disk(struct device *parent, struct gendisk *disk,
209 const struct attribute_group **groups);
add_disk(struct gendisk * disk)210 static inline int add_disk(struct gendisk *disk)
211 {
212 return device_add_disk(NULL, disk, NULL);
213 }
214 extern void del_gendisk(struct gendisk *gp);
215
216 void set_disk_ro(struct gendisk *disk, bool read_only);
217
get_disk_ro(struct gendisk * disk)218 static inline int get_disk_ro(struct gendisk *disk)
219 {
220 return disk->part0->bd_read_only ||
221 test_bit(GD_READ_ONLY, &disk->state);
222 }
223
224 extern void disk_block_events(struct gendisk *disk);
225 extern void disk_unblock_events(struct gendisk *disk);
226 extern void disk_flush_events(struct gendisk *disk, unsigned int mask);
227 bool set_capacity_and_notify(struct gendisk *disk, sector_t size);
228 bool disk_force_media_change(struct gendisk *disk, unsigned int events);
229
230 /* drivers/char/random.c */
231 extern void add_disk_randomness(struct gendisk *disk) __latent_entropy;
232 extern void rand_initialize_disk(struct gendisk *disk);
233
get_start_sect(struct block_device * bdev)234 static inline sector_t get_start_sect(struct block_device *bdev)
235 {
236 return bdev->bd_start_sect;
237 }
238
bdev_nr_sectors(struct block_device * bdev)239 static inline sector_t bdev_nr_sectors(struct block_device *bdev)
240 {
241 return i_size_read(bdev->bd_inode) >> 9;
242 }
243
get_capacity(struct gendisk * disk)244 static inline sector_t get_capacity(struct gendisk *disk)
245 {
246 return bdev_nr_sectors(disk->part0);
247 }
248
249 int bdev_disk_changed(struct gendisk *disk, bool invalidate);
250 void blk_drop_partitions(struct gendisk *disk);
251
252 struct gendisk *__alloc_disk_node(struct request_queue *q, int node_id,
253 struct lock_class_key *lkclass);
254 extern void put_disk(struct gendisk *disk);
255 struct gendisk *__blk_alloc_disk(int node, struct lock_class_key *lkclass);
256
257 /**
258 * blk_alloc_disk - allocate a gendisk structure
259 * @node_id: numa node to allocate on
260 *
261 * Allocate and pre-initialize a gendisk structure for use with BIO based
262 * drivers.
263 *
264 * Context: can sleep
265 */
266 #define blk_alloc_disk(node_id) \
267 ({ \
268 static struct lock_class_key __key; \
269 \
270 __blk_alloc_disk(node_id, &__key); \
271 })
272 void blk_cleanup_disk(struct gendisk *disk);
273
274 int __register_blkdev(unsigned int major, const char *name,
275 void (*probe)(dev_t devt));
276 #define register_blkdev(major, name) \
277 __register_blkdev(major, name, NULL)
278 void unregister_blkdev(unsigned int major, const char *name);
279
280 bool bdev_check_media_change(struct block_device *bdev);
281 int __invalidate_device(struct block_device *bdev, bool kill_dirty);
282 void set_capacity(struct gendisk *disk, sector_t size);
283
284 /* for drivers/char/raw.c: */
285 int blkdev_ioctl(struct block_device *, fmode_t, unsigned, unsigned long);
286 long compat_blkdev_ioctl(struct file *, unsigned, unsigned long);
287
288 #ifdef CONFIG_BLOCK_HOLDER_DEPRECATED
289 int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk);
290 void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk);
291 int bd_register_pending_holders(struct gendisk *disk);
292 #else
bd_link_disk_holder(struct block_device * bdev,struct gendisk * disk)293 static inline int bd_link_disk_holder(struct block_device *bdev,
294 struct gendisk *disk)
295 {
296 return 0;
297 }
bd_unlink_disk_holder(struct block_device * bdev,struct gendisk * disk)298 static inline void bd_unlink_disk_holder(struct block_device *bdev,
299 struct gendisk *disk)
300 {
301 }
bd_register_pending_holders(struct gendisk * disk)302 static inline int bd_register_pending_holders(struct gendisk *disk)
303 {
304 return 0;
305 }
306 #endif /* CONFIG_BLOCK_HOLDER_DEPRECATED */
307
308 dev_t part_devt(struct gendisk *disk, u8 partno);
309 void inc_diskseq(struct gendisk *disk);
310 dev_t blk_lookup_devt(const char *name, int partno);
311 void blk_request_module(dev_t devt);
312 #ifdef CONFIG_BLOCK
313 void printk_all_partitions(void);
314 #else /* CONFIG_BLOCK */
printk_all_partitions(void)315 static inline void printk_all_partitions(void)
316 {
317 }
318 #endif /* CONFIG_BLOCK */
319
320 #endif /* _LINUX_GENHD_H */
321