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