1
2#ifndef _LINUX_GENHD_H
3#define _LINUX_GENHD_H
4
5
6
7
8
9
10
11
12
13#include <linux/types.h>
14#include <linux/kdev_t.h>
15#include <linux/rcupdate.h>
16#include <linux/slab.h>
17#include <linux/percpu-refcount.h>
18#include <linux/uuid.h>
19#include <linux/blk_types.h>
20#include <asm/local.h>
21
22extern const struct device_type disk_type;
23extern struct device_type part_type;
24extern struct class block_class;
25
26#define DISK_MAX_PARTS 256
27#define DISK_NAME_LEN 32
28
29#include <linux/major.h>
30#include <linux/device.h>
31#include <linux/smp.h>
32#include <linux/string.h>
33#include <linux/fs.h>
34#include <linux/workqueue.h>
35#include <linux/xarray.h>
36
37#define PARTITION_META_INFO_VOLNAMELTH 64
38
39
40
41
42#define PARTITION_META_INFO_UUIDLTH (UUID_STRING_LEN + 1)
43
44struct partition_meta_info {
45 char uuid[PARTITION_META_INFO_UUIDLTH];
46 u8 volname[PARTITION_META_INFO_VOLNAMELTH];
47};
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96#define GENHD_FL_REMOVABLE 0x0001
97
98
99#define GENHD_FL_CD 0x0008
100#define GENHD_FL_UP 0x0010
101#define GENHD_FL_SUPPRESS_PARTITION_INFO 0x0020
102#define GENHD_FL_EXT_DEVT 0x0040
103#define GENHD_FL_NATIVE_CAPACITY 0x0080
104#define GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE 0x0100
105#define GENHD_FL_NO_PART_SCAN 0x0200
106#define GENHD_FL_HIDDEN 0x0400
107
108enum {
109 DISK_EVENT_MEDIA_CHANGE = 1 << 0,
110 DISK_EVENT_EJECT_REQUEST = 1 << 1,
111};
112
113enum {
114
115 DISK_EVENT_FLAG_POLL = 1 << 0,
116
117 DISK_EVENT_FLAG_UEVENT = 1 << 1,
118};
119
120struct disk_events;
121struct badblocks;
122
123struct blk_integrity {
124 const struct blk_integrity_profile *profile;
125 unsigned char flags;
126 unsigned char tuple_size;
127 unsigned char interval_exp;
128 unsigned char tag_size;
129};
130
131struct gendisk {
132
133
134
135 int major;
136 int first_minor;
137 int minors;
138
139
140 char disk_name[DISK_NAME_LEN];
141
142 unsigned short events;
143 unsigned short event_flags;
144
145 struct xarray part_tbl;
146 struct block_device *part0;
147
148 const struct block_device_operations *fops;
149 struct request_queue *queue;
150 void *private_data;
151
152 int flags;
153 unsigned long state;
154#define GD_NEED_PART_SCAN 0
155#define GD_READ_ONLY 1
156#define GD_QUEUE_REF 2
157
158 struct mutex open_mutex;
159 unsigned open_partitions;
160
161 struct kobject *slave_dir;
162
163 struct timer_rand_state *random;
164 atomic_t sync_io;
165 struct disk_events *ev;
166#ifdef CONFIG_BLK_DEV_INTEGRITY
167 struct kobject integrity_kobj;
168#endif
169#if IS_ENABLED(CONFIG_CDROM)
170 struct cdrom_device_info *cdi;
171#endif
172 int node_id;
173 struct badblocks *bb;
174 struct lockdep_map lockdep_map;
175};
176
177
178
179
180
181#define dev_to_disk(device) \
182 (dev_to_bdev(device)->bd_disk)
183#define disk_to_dev(disk) \
184 (&((disk)->part0->bd_device))
185
186#if IS_REACHABLE(CONFIG_CDROM)
187#define disk_to_cdi(disk) ((disk)->cdi)
188#else
189#define disk_to_cdi(disk) NULL
190#endif
191
192static inline int disk_max_parts(struct gendisk *disk)
193{
194 if (disk->flags & GENHD_FL_EXT_DEVT)
195 return DISK_MAX_PARTS;
196 return disk->minors;
197}
198
199static inline bool disk_part_scan_enabled(struct gendisk *disk)
200{
201 return disk_max_parts(disk) > 1 &&
202 !(disk->flags & GENHD_FL_NO_PART_SCAN);
203}
204
205static inline dev_t disk_devt(struct gendisk *disk)
206{
207 return MKDEV(disk->major, disk->first_minor);
208}
209
210void disk_uevent(struct gendisk *disk, enum kobject_action action);
211
212
213extern void device_add_disk(struct device *parent, struct gendisk *disk,
214 const struct attribute_group **groups);
215static inline void add_disk(struct gendisk *disk)
216{
217 device_add_disk(NULL, disk, NULL);
218}
219extern void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk);
220static inline void add_disk_no_queue_reg(struct gendisk *disk)
221{
222 device_add_disk_no_queue_reg(NULL, disk);
223}
224
225extern void del_gendisk(struct gendisk *gp);
226
227void set_disk_ro(struct gendisk *disk, bool read_only);
228
229static inline int get_disk_ro(struct gendisk *disk)
230{
231 return disk->part0->bd_read_only ||
232 test_bit(GD_READ_ONLY, &disk->state);
233}
234
235extern void disk_block_events(struct gendisk *disk);
236extern void disk_unblock_events(struct gendisk *disk);
237extern void disk_flush_events(struct gendisk *disk, unsigned int mask);
238bool set_capacity_and_notify(struct gendisk *disk, sector_t size);
239
240
241extern void add_disk_randomness(struct gendisk *disk) __latent_entropy;
242extern void rand_initialize_disk(struct gendisk *disk);
243
244static inline sector_t get_start_sect(struct block_device *bdev)
245{
246 return bdev->bd_start_sect;
247}
248
249static inline sector_t bdev_nr_sectors(struct block_device *bdev)
250{
251 return i_size_read(bdev->bd_inode) >> 9;
252}
253
254static inline sector_t get_capacity(struct gendisk *disk)
255{
256 return bdev_nr_sectors(disk->part0);
257}
258
259int bdev_disk_changed(struct gendisk *disk, bool invalidate);
260void blk_drop_partitions(struct gendisk *disk);
261
262extern struct gendisk *__alloc_disk_node(int minors, int node_id);
263extern void put_disk(struct gendisk *disk);
264
265#define alloc_disk_node(minors, node_id) \
266({ \
267 static struct lock_class_key __key; \
268 const char *__name; \
269 struct gendisk *__disk; \
270 \
271 __name = "(gendisk_completion)"#minors"("#node_id")"; \
272 \
273 __disk = __alloc_disk_node(minors, node_id); \
274 \
275 if (__disk) \
276 lockdep_init_map(&__disk->lockdep_map, __name, &__key, 0); \
277 \
278 __disk; \
279})
280
281#define alloc_disk(minors) alloc_disk_node(minors, NUMA_NO_NODE)
282
283
284
285
286
287
288
289
290
291
292#define blk_alloc_disk(node_id) \
293({ \
294 struct gendisk *__disk = __blk_alloc_disk(node_id); \
295 static struct lock_class_key __key; \
296 \
297 if (__disk) \
298 lockdep_init_map(&__disk->lockdep_map, \
299 "(bio completion)", &__key, 0); \
300 __disk; \
301})
302struct gendisk *__blk_alloc_disk(int node);
303void blk_cleanup_disk(struct gendisk *disk);
304
305int __register_blkdev(unsigned int major, const char *name,
306 void (*probe)(dev_t devt));
307#define register_blkdev(major, name) \
308 __register_blkdev(major, name, NULL)
309void unregister_blkdev(unsigned int major, const char *name);
310
311bool bdev_check_media_change(struct block_device *bdev);
312int __invalidate_device(struct block_device *bdev, bool kill_dirty);
313void set_capacity(struct gendisk *disk, sector_t size);
314
315
316int blkdev_ioctl(struct block_device *, fmode_t, unsigned, unsigned long);
317long compat_blkdev_ioctl(struct file *, unsigned, unsigned long);
318
319#ifdef CONFIG_SYSFS
320int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk);
321void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk);
322#else
323static inline int bd_link_disk_holder(struct block_device *bdev,
324 struct gendisk *disk)
325{
326 return 0;
327}
328static inline void bd_unlink_disk_holder(struct block_device *bdev,
329 struct gendisk *disk)
330{
331}
332#endif
333
334dev_t part_devt(struct gendisk *disk, u8 partno);
335dev_t blk_lookup_devt(const char *name, int partno);
336void blk_request_module(dev_t devt);
337#ifdef CONFIG_BLOCK
338void printk_all_partitions(void);
339#else
340static inline void printk_all_partitions(void)
341{
342}
343#endif
344
345#endif
346