Line data Source code
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/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 :
22 : extern const struct device_type disk_type;
23 : extern struct device_type part_type;
24 : extern 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 : * Enough for the string representation of any kind of UUID plus NULL.
40 : * EFI UUID is 36 characters. MSDOS UUID is 11 characters.
41 : */
42 : #define PARTITION_META_INFO_UUIDLTH (UUID_STRING_LEN + 1)
43 :
44 : struct partition_meta_info {
45 : char uuid[PARTITION_META_INFO_UUIDLTH];
46 : u8 volname[PARTITION_META_INFO_VOLNAMELTH];
47 : };
48 :
49 : /**
50 : * DOC: genhd capability flags
51 : *
52 : * ``GENHD_FL_REMOVABLE`` (0x0001): indicates that the block device
53 : * gives access to removable media.
54 : * When set, the device remains present even when media is not
55 : * inserted.
56 : * Must not be set for devices which are removed entirely when the
57 : * media is removed.
58 : *
59 : * ``GENHD_FL_CD`` (0x0008): the block device is a CD-ROM-style
60 : * device.
61 : * Affects responses to the ``CDROM_GET_CAPABILITY`` ioctl.
62 : *
63 : * ``GENHD_FL_UP`` (0x0010): indicates that the block device is "up",
64 : * with a similar meaning to network interfaces.
65 : *
66 : * ``GENHD_FL_SUPPRESS_PARTITION_INFO`` (0x0020): don't include
67 : * partition information in ``/proc/partitions`` or in the output of
68 : * printk_all_partitions().
69 : * Used for the null block device and some MMC devices.
70 : *
71 : * ``GENHD_FL_EXT_DEVT`` (0x0040): the driver supports extended
72 : * dynamic ``dev_t``, i.e. it wants extended device numbers
73 : * (``BLOCK_EXT_MAJOR``).
74 : * This affects the maximum number of partitions.
75 : *
76 : * ``GENHD_FL_NATIVE_CAPACITY`` (0x0080): based on information in the
77 : * partition table, the device's capacity has been extended to its
78 : * native capacity; i.e. the device has hidden capacity used by one
79 : * of the partitions (this is a flag used so that native capacity is
80 : * only ever unlocked once).
81 : *
82 : * ``GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE`` (0x0100): event polling is
83 : * blocked whenever a writer holds an exclusive lock.
84 : *
85 : * ``GENHD_FL_NO_PART_SCAN`` (0x0200): partition scanning is disabled.
86 : * Used for loop devices in their default settings and some MMC
87 : * devices.
88 : *
89 : * ``GENHD_FL_HIDDEN`` (0x0400): the block device is hidden; it
90 : * doesn't produce events, doesn't appear in sysfs, and doesn't have
91 : * an associated ``bdev``.
92 : * Implies ``GENHD_FL_SUPPRESS_PARTITION_INFO`` and
93 : * ``GENHD_FL_NO_PART_SCAN``.
94 : * Used for multipath devices.
95 : */
96 : #define GENHD_FL_REMOVABLE 0x0001
97 : /* 2 is unused (used to be GENHD_FL_DRIVERFS) */
98 : /* 4 is unused (used to be GENHD_FL_MEDIA_CHANGE_NOTIFY) */
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 :
108 : enum {
109 : DISK_EVENT_MEDIA_CHANGE = 1 << 0, /* media changed */
110 : DISK_EVENT_EJECT_REQUEST = 1 << 1, /* eject requested */
111 : };
112 :
113 : enum {
114 : /* Poll even if events_poll_msecs is unset */
115 : DISK_EVENT_FLAG_POLL = 1 << 0,
116 : /* Forward events to udev */
117 : DISK_EVENT_FLAG_UEVENT = 1 << 1,
118 : };
119 :
120 : struct disk_events;
121 : struct badblocks;
122 :
123 : struct 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 :
131 : struct gendisk {
132 : /* major, first_minor and minors are input parameters only,
133 : * don't use directly. Use disk_devt() and disk_max_parts().
134 : */
135 : int major; /* major number of driver */
136 : int first_minor;
137 : int minors; /* maximum number of minors, =1 for
138 : * disks that can't be partitioned. */
139 :
140 : char disk_name[DISK_NAME_LEN]; /* name of major driver */
141 :
142 : unsigned short events; /* supported events */
143 : unsigned short event_flags; /* flags related to event processing */
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 : struct kobject *slave_dir;
157 :
158 : struct timer_rand_state *random;
159 : atomic_t sync_io; /* RAID */
160 : struct disk_events *ev;
161 : #ifdef CONFIG_BLK_DEV_INTEGRITY
162 : struct kobject integrity_kobj;
163 : #endif /* CONFIG_BLK_DEV_INTEGRITY */
164 : #if IS_ENABLED(CONFIG_CDROM)
165 : struct cdrom_device_info *cdi;
166 : #endif
167 : int node_id;
168 : struct badblocks *bb;
169 : struct lockdep_map lockdep_map;
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 :
187 4 : static inline int disk_max_parts(struct gendisk *disk)
188 : {
189 2 : if (disk->flags & GENHD_FL_EXT_DEVT)
190 : return DISK_MAX_PARTS;
191 0 : return disk->minors;
192 : }
193 :
194 2 : static inline bool disk_part_scan_enabled(struct gendisk *disk)
195 : {
196 4 : return disk_max_parts(disk) > 1 &&
197 2 : !(disk->flags & GENHD_FL_NO_PART_SCAN);
198 : }
199 :
200 1 : static inline dev_t disk_devt(struct gendisk *disk)
201 : {
202 1 : return MKDEV(disk->major, disk->first_minor);
203 : }
204 :
205 : void disk_uevent(struct gendisk *disk, enum kobject_action action);
206 :
207 : /*
208 : * Smarter partition iterator without context limits.
209 : */
210 : #define DISK_PITER_INCL_EMPTY (1 << 1) /* include 0-sized parts */
211 : #define DISK_PITER_INCL_PART0 (1 << 2) /* include partition 0 */
212 : #define DISK_PITER_INCL_EMPTY_PART0 (1 << 3) /* include empty partition 0 */
213 :
214 : struct disk_part_iter {
215 : struct gendisk *disk;
216 : struct block_device *part;
217 : unsigned long idx;
218 : unsigned int flags;
219 : };
220 :
221 : extern void disk_part_iter_init(struct disk_part_iter *piter,
222 : struct gendisk *disk, unsigned int flags);
223 : struct block_device *disk_part_iter_next(struct disk_part_iter *piter);
224 : extern void disk_part_iter_exit(struct disk_part_iter *piter);
225 :
226 : /* block/genhd.c */
227 : extern void device_add_disk(struct device *parent, struct gendisk *disk,
228 : const struct attribute_group **groups);
229 8 : static inline void add_disk(struct gendisk *disk)
230 : {
231 8 : device_add_disk(NULL, disk, NULL);
232 : }
233 : extern void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk);
234 0 : static inline void add_disk_no_queue_reg(struct gendisk *disk)
235 : {
236 0 : device_add_disk_no_queue_reg(NULL, disk);
237 : }
238 :
239 : extern void del_gendisk(struct gendisk *gp);
240 : extern struct block_device *bdget_disk(struct gendisk *disk, int partno);
241 :
242 : void set_disk_ro(struct gendisk *disk, bool read_only);
243 :
244 3079 : static inline int get_disk_ro(struct gendisk *disk)
245 : {
246 3079 : return disk->part0->bd_read_only ||
247 3079 : test_bit(GD_READ_ONLY, &disk->state);
248 : }
249 :
250 : extern void disk_block_events(struct gendisk *disk);
251 : extern void disk_unblock_events(struct gendisk *disk);
252 : extern void disk_flush_events(struct gendisk *disk, unsigned int mask);
253 : bool set_capacity_and_notify(struct gendisk *disk, sector_t size);
254 :
255 : /* drivers/char/random.c */
256 : extern void add_disk_randomness(struct gendisk *disk) __latent_entropy;
257 : extern void rand_initialize_disk(struct gendisk *disk);
258 :
259 0 : static inline sector_t get_start_sect(struct block_device *bdev)
260 : {
261 0 : return bdev->bd_start_sect;
262 : }
263 :
264 15638 : static inline sector_t bdev_nr_sectors(struct block_device *bdev)
265 : {
266 15612 : return i_size_read(bdev->bd_inode) >> 9;
267 : }
268 :
269 23 : static inline sector_t get_capacity(struct gendisk *disk)
270 : {
271 22 : return bdev_nr_sectors(disk->part0);
272 : }
273 :
274 : int bdev_disk_changed(struct block_device *bdev, bool invalidate);
275 : int blk_add_partitions(struct gendisk *disk, struct block_device *bdev);
276 : int blk_drop_partitions(struct block_device *bdev);
277 :
278 : extern struct gendisk *__alloc_disk_node(int minors, int node_id);
279 : extern void put_disk(struct gendisk *disk);
280 :
281 : #define alloc_disk_node(minors, node_id) \
282 : ({ \
283 : static struct lock_class_key __key; \
284 : const char *__name; \
285 : struct gendisk *__disk; \
286 : \
287 : __name = "(gendisk_completion)"#minors"("#node_id")"; \
288 : \
289 : __disk = __alloc_disk_node(minors, node_id); \
290 : \
291 : if (__disk) \
292 : lockdep_init_map(&__disk->lockdep_map, __name, &__key, 0); \
293 : \
294 : __disk; \
295 : })
296 :
297 : #define alloc_disk(minors) alloc_disk_node(minors, NUMA_NO_NODE)
298 :
299 : int __register_blkdev(unsigned int major, const char *name,
300 : void (*probe)(dev_t devt));
301 : #define register_blkdev(major, name) \
302 : __register_blkdev(major, name, NULL)
303 : void unregister_blkdev(unsigned int major, const char *name);
304 :
305 : bool bdev_check_media_change(struct block_device *bdev);
306 : int __invalidate_device(struct block_device *bdev, bool kill_dirty);
307 : void set_capacity(struct gendisk *disk, sector_t size);
308 :
309 : /* for drivers/char/raw.c: */
310 : int blkdev_ioctl(struct block_device *, fmode_t, unsigned, unsigned long);
311 : long compat_blkdev_ioctl(struct file *, unsigned, unsigned long);
312 :
313 : #ifdef CONFIG_SYSFS
314 : int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk);
315 : void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk);
316 : #else
317 : static inline int bd_link_disk_holder(struct block_device *bdev,
318 : struct gendisk *disk)
319 : {
320 : return 0;
321 : }
322 : static inline void bd_unlink_disk_holder(struct block_device *bdev,
323 : struct gendisk *disk)
324 : {
325 : }
326 : #endif /* CONFIG_SYSFS */
327 :
328 : extern struct rw_semaphore bdev_lookup_sem;
329 :
330 : dev_t blk_lookup_devt(const char *name, int partno);
331 : void blk_request_module(dev_t devt);
332 : #ifdef CONFIG_BLOCK
333 : void printk_all_partitions(void);
334 : #else /* CONFIG_BLOCK */
335 : static inline void printk_all_partitions(void)
336 : {
337 : }
338 : #endif /* CONFIG_BLOCK */
339 :
340 : #endif /* _LINUX_GENHD_H */
|