1 /* SPDX-License-Identifier: GPL-2.0 */
6 * genhd.h Copyright (C) 1992 Drew Eckhardt
7 * Generic hard disk header file by
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>
24 #define dev_to_disk(device) container_of((device), struct gendisk, part0.__dev)
25 #define dev_to_part(device) container_of((device), struct hd_struct, __dev)
26 #define disk_to_dev(disk) (&(disk)->part0.__dev)
27 #define part_to_dev(part) (&((part)->__dev))
29 extern struct device_type part_type;
30 extern struct class block_class;
32 #define DISK_MAX_PARTS 256
33 #define DISK_NAME_LEN 32
35 #include <linux/major.h>
36 #include <linux/device.h>
37 #include <linux/smp.h>
38 #include <linux/string.h>
40 #include <linux/workqueue.h>
43 u64 nsecs[NR_STAT_GROUPS];
44 unsigned long sectors[NR_STAT_GROUPS];
45 unsigned long ios[NR_STAT_GROUPS];
46 unsigned long merges[NR_STAT_GROUPS];
47 unsigned long io_ticks;
51 #define PARTITION_META_INFO_VOLNAMELTH 64
53 * Enough for the string representation of any kind of UUID plus NULL.
54 * EFI UUID is 36 characters. MSDOS UUID is 11 characters.
56 #define PARTITION_META_INFO_UUIDLTH (UUID_STRING_LEN + 1)
58 struct partition_meta_info {
59 char uuid[PARTITION_META_INFO_UUIDLTH];
60 u8 volname[PARTITION_META_INFO_VOLNAMELTH];
66 * nr_sects is protected by sequence counter. One might extend a
67 * partition while IO is happening to it and update of nr_sects
68 * can be non-atomic on 32bit machines with 64bit sector_t.
71 seqcount_t nr_sects_seq;
72 sector_t alignment_offset;
73 unsigned int discard_alignment;
75 struct kobject *holder_dir;
77 struct partition_meta_info *info;
78 #ifdef CONFIG_FAIL_MAKE_REQUEST
83 struct disk_stats __percpu *dkstats;
85 struct disk_stats dkstats;
87 struct percpu_ref ref;
88 struct rcu_work rcu_work;
92 * DOC: genhd capability flags
94 * ``GENHD_FL_REMOVABLE`` (0x0001): indicates that the block device
95 * gives access to removable media.
96 * When set, the device remains present even when media is not
98 * Must not be set for devices which are removed entirely when the
101 * ``GENHD_FL_CD`` (0x0008): the block device is a CD-ROM-style
103 * Affects responses to the ``CDROM_GET_CAPABILITY`` ioctl.
105 * ``GENHD_FL_UP`` (0x0010): indicates that the block device is "up",
106 * with a similar meaning to network interfaces.
108 * ``GENHD_FL_SUPPRESS_PARTITION_INFO`` (0x0020): don't include
109 * partition information in ``/proc/partitions`` or in the output of
110 * printk_all_partitions().
111 * Used for the null block device and some MMC devices.
113 * ``GENHD_FL_EXT_DEVT`` (0x0040): the driver supports extended
114 * dynamic ``dev_t``, i.e. it wants extended device numbers
115 * (``BLOCK_EXT_MAJOR``).
116 * This affects the maximum number of partitions.
118 * ``GENHD_FL_NATIVE_CAPACITY`` (0x0080): based on information in the
119 * partition table, the device's capacity has been extended to its
120 * native capacity; i.e. the device has hidden capacity used by one
121 * of the partitions (this is a flag used so that native capacity is
122 * only ever unlocked once).
124 * ``GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE`` (0x0100): event polling is
125 * blocked whenever a writer holds an exclusive lock.
127 * ``GENHD_FL_NO_PART_SCAN`` (0x0200): partition scanning is disabled.
128 * Used for loop devices in their default settings and some MMC
131 * ``GENHD_FL_HIDDEN`` (0x0400): the block device is hidden; it
132 * doesn't produce events, doesn't appear in sysfs, and doesn't have
133 * an associated ``bdev``.
134 * Implies ``GENHD_FL_SUPPRESS_PARTITION_INFO`` and
135 * ``GENHD_FL_NO_PART_SCAN``.
136 * Used for multipath devices.
138 #define GENHD_FL_REMOVABLE 0x0001
139 /* 2 is unused (used to be GENHD_FL_DRIVERFS) */
140 /* 4 is unused (used to be GENHD_FL_MEDIA_CHANGE_NOTIFY) */
141 #define GENHD_FL_CD 0x0008
142 #define GENHD_FL_UP 0x0010
143 #define GENHD_FL_SUPPRESS_PARTITION_INFO 0x0020
144 #define GENHD_FL_EXT_DEVT 0x0040
145 #define GENHD_FL_NATIVE_CAPACITY 0x0080
146 #define GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE 0x0100
147 #define GENHD_FL_NO_PART_SCAN 0x0200
148 #define GENHD_FL_HIDDEN 0x0400
151 DISK_EVENT_MEDIA_CHANGE = 1 << 0, /* media changed */
152 DISK_EVENT_EJECT_REQUEST = 1 << 1, /* eject requested */
156 /* Poll even if events_poll_msecs is unset */
157 DISK_EVENT_FLAG_POLL = 1 << 0,
158 /* Forward events to udev */
159 DISK_EVENT_FLAG_UEVENT = 1 << 1,
162 struct disk_part_tbl {
163 struct rcu_head rcu_head;
165 struct hd_struct __rcu *last_lookup;
166 struct hd_struct __rcu *part[];
172 struct blk_integrity {
173 const struct blk_integrity_profile *profile;
175 unsigned char tuple_size;
176 unsigned char interval_exp;
177 unsigned char tag_size;
181 /* major, first_minor and minors are input parameters only,
182 * don't use directly. Use disk_devt() and disk_max_parts().
184 int major; /* major number of driver */
186 int minors; /* maximum number of minors, =1 for
187 * disks that can't be partitioned. */
189 char disk_name[DISK_NAME_LEN]; /* name of major driver */
191 unsigned short events; /* supported events */
192 unsigned short event_flags; /* flags related to event processing */
194 /* Array of pointers to partitions indexed by partno.
195 * Protected with matching bdev lock but stat and other
196 * non-critical accesses use RCU. Always access through
199 struct disk_part_tbl __rcu *part_tbl;
200 struct hd_struct part0;
202 const struct block_device_operations *fops;
203 struct request_queue *queue;
207 struct rw_semaphore lookup_sem;
208 struct kobject *slave_dir;
210 struct timer_rand_state *random;
211 atomic_t sync_io; /* RAID */
212 struct disk_events *ev;
213 #ifdef CONFIG_BLK_DEV_INTEGRITY
214 struct kobject integrity_kobj;
215 #endif /* CONFIG_BLK_DEV_INTEGRITY */
216 #if IS_ENABLED(CONFIG_CDROM)
217 struct cdrom_device_info *cdi;
220 struct badblocks *bb;
221 struct lockdep_map lockdep_map;
224 #if IS_REACHABLE(CONFIG_CDROM)
225 #define disk_to_cdi(disk) ((disk)->cdi)
227 #define disk_to_cdi(disk) NULL
230 static inline struct gendisk *part_to_disk(struct hd_struct *part)
234 return dev_to_disk(part_to_dev(part)->parent);
236 return dev_to_disk(part_to_dev(part));
241 static inline int disk_max_parts(struct gendisk *disk)
243 if (disk->flags & GENHD_FL_EXT_DEVT)
244 return DISK_MAX_PARTS;
248 static inline bool disk_part_scan_enabled(struct gendisk *disk)
250 return disk_max_parts(disk) > 1 &&
251 !(disk->flags & GENHD_FL_NO_PART_SCAN);
254 static inline dev_t disk_devt(struct gendisk *disk)
256 return MKDEV(disk->major, disk->first_minor);
259 static inline dev_t part_devt(struct hd_struct *part)
261 return part_to_dev(part)->devt;
264 extern struct hd_struct *__disk_get_part(struct gendisk *disk, int partno);
265 extern struct hd_struct *disk_get_part(struct gendisk *disk, int partno);
267 static inline void disk_put_part(struct hd_struct *part)
270 put_device(part_to_dev(part));
274 * Smarter partition iterator without context limits.
276 #define DISK_PITER_REVERSE (1 << 0) /* iterate in the reverse direction */
277 #define DISK_PITER_INCL_EMPTY (1 << 1) /* include 0-sized parts */
278 #define DISK_PITER_INCL_PART0 (1 << 2) /* include partition 0 */
279 #define DISK_PITER_INCL_EMPTY_PART0 (1 << 3) /* include empty partition 0 */
281 struct disk_part_iter {
282 struct gendisk *disk;
283 struct hd_struct *part;
288 extern void disk_part_iter_init(struct disk_part_iter *piter,
289 struct gendisk *disk, unsigned int flags);
290 extern struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter);
291 extern void disk_part_iter_exit(struct disk_part_iter *piter);
292 extern bool disk_has_partitions(struct gendisk *disk);
295 extern void device_add_disk(struct device *parent, struct gendisk *disk,
296 const struct attribute_group **groups);
297 static inline void add_disk(struct gendisk *disk)
299 device_add_disk(NULL, disk, NULL);
301 extern void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk);
302 static inline void add_disk_no_queue_reg(struct gendisk *disk)
304 device_add_disk_no_queue_reg(NULL, disk);
307 extern void del_gendisk(struct gendisk *gp);
308 extern struct gendisk *get_gendisk(dev_t dev, int *partno);
309 extern struct block_device *bdget_disk(struct gendisk *disk, int partno);
311 extern void set_device_ro(struct block_device *bdev, int flag);
312 extern void set_disk_ro(struct gendisk *disk, int flag);
314 static inline int get_disk_ro(struct gendisk *disk)
316 return disk->part0.policy;
319 extern void disk_block_events(struct gendisk *disk);
320 extern void disk_unblock_events(struct gendisk *disk);
321 extern void disk_flush_events(struct gendisk *disk, unsigned int mask);
322 extern void set_capacity_revalidate_and_notify(struct gendisk *disk,
323 sector_t size, bool revalidate);
324 extern unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask);
326 /* drivers/char/random.c */
327 extern void add_disk_randomness(struct gendisk *disk) __latent_entropy;
328 extern void rand_initialize_disk(struct gendisk *disk);
330 static inline sector_t get_start_sect(struct block_device *bdev)
332 return bdev->bd_part->start_sect;
334 static inline sector_t get_capacity(struct gendisk *disk)
336 return disk->part0.nr_sects;
338 static inline void set_capacity(struct gendisk *disk, sector_t size)
340 disk->part0.nr_sects = size;
343 extern dev_t blk_lookup_devt(const char *name, int partno);
345 int bdev_disk_changed(struct block_device *bdev, bool invalidate);
346 int blk_add_partitions(struct gendisk *disk, struct block_device *bdev);
347 int blk_drop_partitions(struct block_device *bdev);
348 extern void printk_all_partitions(void);
350 extern struct gendisk *__alloc_disk_node(int minors, int node_id);
351 extern struct kobject *get_disk_and_module(struct gendisk *disk);
352 extern void put_disk(struct gendisk *disk);
353 extern void put_disk_and_module(struct gendisk *disk);
354 extern void blk_register_region(dev_t devt, unsigned long range,
355 struct module *module,
356 struct kobject *(*probe)(dev_t, int *, void *),
357 int (*lock)(dev_t, void *),
359 extern void blk_unregister_region(dev_t devt, unsigned long range);
361 #define alloc_disk_node(minors, node_id) \
363 static struct lock_class_key __key; \
364 const char *__name; \
365 struct gendisk *__disk; \
367 __name = "(gendisk_completion)"#minors"("#node_id")"; \
369 __disk = __alloc_disk_node(minors, node_id); \
372 lockdep_init_map(&__disk->lockdep_map, __name, &__key, 0); \
377 #define alloc_disk(minors) alloc_disk_node(minors, NUMA_NO_NODE)
379 #else /* CONFIG_BLOCK */
381 static inline void printk_all_partitions(void) { }
383 static inline dev_t blk_lookup_devt(const char *name, int partno)
385 dev_t devt = MKDEV(0, 0);
388 #endif /* CONFIG_BLOCK */
390 #endif /* _LINUX_GENHD_H */