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 <linux/android_kabi.h>
21 #include <asm/local.h>
22
23 #define dev_to_disk(device) container_of((device), struct gendisk, part0.__dev)
24 #define dev_to_part(device) container_of((device), struct hd_struct, __dev)
25 #define disk_to_dev(disk) (&(disk)->part0.__dev)
26 #define part_to_dev(part) (&((part)->__dev))
27
28 extern const struct device_type disk_type;
29 extern struct device_type part_type;
30 extern struct class block_class;
31
32 #define DISK_MAX_PARTS 256
33 #define DISK_NAME_LEN 32
34
35 #include <linux/major.h>
36 #include <linux/device.h>
37 #include <linux/smp.h>
38 #include <linux/string.h>
39 #include <linux/fs.h>
40 #include <linux/workqueue.h>
41
42 #define PARTITION_META_INFO_VOLNAMELTH 64
43 /*
44 * Enough for the string representation of any kind of UUID plus NULL.
45 * EFI UUID is 36 characters. MSDOS UUID is 11 characters.
46 */
47 #define PARTITION_META_INFO_UUIDLTH (UUID_STRING_LEN + 1)
48
49 struct partition_meta_info {
50 char uuid[PARTITION_META_INFO_UUIDLTH];
51 u8 volname[PARTITION_META_INFO_VOLNAMELTH];
52 };
53
54 struct hd_struct {
55 sector_t start_sect;
56 /*
57 * nr_sects is protected by sequence counter. One might extend a
58 * partition while IO is happening to it and update of nr_sects
59 * can be non-atomic on 32bit machines with 64bit sector_t.
60 */
61 sector_t nr_sects;
62 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
63 seqcount_t nr_sects_seq;
64 #endif
65 unsigned long stamp;
66 struct disk_stats __percpu *dkstats;
67 struct percpu_ref ref;
68
69 struct device __dev;
70 struct kobject *holder_dir;
71 int policy, partno;
72 struct partition_meta_info *info;
73 #ifdef CONFIG_FAIL_MAKE_REQUEST
74 int make_it_fail;
75 #endif
76 struct rcu_work rcu_work;
77
78 ANDROID_KABI_RESERVE(1);
79 ANDROID_KABI_RESERVE(2);
80 ANDROID_KABI_RESERVE(3);
81 ANDROID_KABI_RESERVE(4);
82 };
83
84 /**
85 * DOC: genhd capability flags
86 *
87 * ``GENHD_FL_REMOVABLE`` (0x0001): indicates that the block device
88 * gives access to removable media.
89 * When set, the device remains present even when media is not
90 * inserted.
91 * Must not be set for devices which are removed entirely when the
92 * media is removed.
93 *
94 * ``GENHD_FL_CD`` (0x0008): the block device is a CD-ROM-style
95 * device.
96 * Affects responses to the ``CDROM_GET_CAPABILITY`` ioctl.
97 *
98 * ``GENHD_FL_UP`` (0x0010): indicates that the block device is "up",
99 * with a similar meaning to network interfaces.
100 *
101 * ``GENHD_FL_SUPPRESS_PARTITION_INFO`` (0x0020): don't include
102 * partition information in ``/proc/partitions`` or in the output of
103 * printk_all_partitions().
104 * Used for the null block device and some MMC devices.
105 *
106 * ``GENHD_FL_EXT_DEVT`` (0x0040): the driver supports extended
107 * dynamic ``dev_t``, i.e. it wants extended device numbers
108 * (``BLOCK_EXT_MAJOR``).
109 * This affects the maximum number of partitions.
110 *
111 * ``GENHD_FL_NATIVE_CAPACITY`` (0x0080): based on information in the
112 * partition table, the device's capacity has been extended to its
113 * native capacity; i.e. the device has hidden capacity used by one
114 * of the partitions (this is a flag used so that native capacity is
115 * only ever unlocked once).
116 *
117 * ``GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE`` (0x0100): event polling is
118 * blocked whenever a writer holds an exclusive lock.
119 *
120 * ``GENHD_FL_NO_PART_SCAN`` (0x0200): partition scanning is disabled.
121 * Used for loop devices in their default settings and some MMC
122 * devices.
123 *
124 * ``GENHD_FL_HIDDEN`` (0x0400): the block device is hidden; it
125 * doesn't produce events, doesn't appear in sysfs, and doesn't have
126 * an associated ``bdev``.
127 * Implies ``GENHD_FL_SUPPRESS_PARTITION_INFO`` and
128 * ``GENHD_FL_NO_PART_SCAN``.
129 * Used for multipath devices.
130 */
131 #define GENHD_FL_REMOVABLE 0x0001
132 /* 2 is unused (used to be GENHD_FL_DRIVERFS) */
133 /* 4 is unused (used to be GENHD_FL_MEDIA_CHANGE_NOTIFY) */
134 #define GENHD_FL_CD 0x0008
135 #define GENHD_FL_UP 0x0010
136 #define GENHD_FL_SUPPRESS_PARTITION_INFO 0x0020
137 #define GENHD_FL_EXT_DEVT 0x0040
138 #define GENHD_FL_NATIVE_CAPACITY 0x0080
139 #define GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE 0x0100
140 #define GENHD_FL_NO_PART_SCAN 0x0200
141 #define GENHD_FL_HIDDEN 0x0400
142
143 enum {
144 DISK_EVENT_MEDIA_CHANGE = 1 << 0, /* media changed */
145 DISK_EVENT_EJECT_REQUEST = 1 << 1, /* eject requested */
146 };
147
148 enum {
149 /* Poll even if events_poll_msecs is unset */
150 DISK_EVENT_FLAG_POLL = 1 << 0,
151 /* Forward events to udev */
152 DISK_EVENT_FLAG_UEVENT = 1 << 1,
153 };
154
155 struct disk_part_tbl {
156 struct rcu_head rcu_head;
157 int len;
158 struct hd_struct __rcu *last_lookup;
159 struct hd_struct __rcu *part[];
160 };
161
162 struct disk_events;
163 struct badblocks;
164
165 struct blk_integrity {
166 const struct blk_integrity_profile *profile;
167 unsigned char flags;
168 unsigned char tuple_size;
169 unsigned char interval_exp;
170 unsigned char tag_size;
171
172 ANDROID_KABI_RESERVE(1);
173 ANDROID_KABI_RESERVE(2);
174 };
175
176 struct gendisk {
177 /* major, first_minor and minors are input parameters only,
178 * don't use directly. Use disk_devt() and disk_max_parts().
179 */
180 int major; /* major number of driver */
181 int first_minor;
182 int minors; /* maximum number of minors, =1 for
183 * disks that can't be partitioned. */
184
185 char disk_name[DISK_NAME_LEN]; /* name of major driver */
186
187 unsigned short events; /* supported events */
188 unsigned short event_flags; /* flags related to event processing */
189
190 /* Array of pointers to partitions indexed by partno.
191 * Protected with matching bdev lock but stat and other
192 * non-critical accesses use RCU. Always access through
193 * helpers.
194 */
195 struct disk_part_tbl __rcu *part_tbl;
196 struct hd_struct part0;
197
198 const struct block_device_operations *fops;
199 struct request_queue *queue;
200 void *private_data;
201
202 int flags;
203 unsigned long state;
204 #define GD_NEED_PART_SCAN 0
205 struct rw_semaphore lookup_sem;
206 struct kobject *slave_dir;
207
208 struct timer_rand_state *random;
209 atomic_t sync_io; /* RAID */
210 struct disk_events *ev;
211 #ifdef CONFIG_BLK_DEV_INTEGRITY
212 struct kobject integrity_kobj;
213 #endif /* CONFIG_BLK_DEV_INTEGRITY */
214 #if IS_ENABLED(CONFIG_CDROM)
215 struct cdrom_device_info *cdi;
216 #endif
217 int node_id;
218 struct badblocks *bb;
219 struct lockdep_map lockdep_map;
220
221 ANDROID_KABI_RESERVE(1);
222 ANDROID_KABI_RESERVE(2);
223 ANDROID_KABI_RESERVE(3);
224 ANDROID_KABI_RESERVE(4);
225
226 };
227
228 #if IS_REACHABLE(CONFIG_CDROM)
229 #define disk_to_cdi(disk) ((disk)->cdi)
230 #else
231 #define disk_to_cdi(disk) NULL
232 #endif
233
part_to_disk(struct hd_struct * part)234 static inline struct gendisk *part_to_disk(struct hd_struct *part)
235 {
236 if (likely(part)) {
237 if (part->partno)
238 return dev_to_disk(part_to_dev(part)->parent);
239 else
240 return dev_to_disk(part_to_dev(part));
241 }
242 return NULL;
243 }
244
disk_max_parts(struct gendisk * disk)245 static inline int disk_max_parts(struct gendisk *disk)
246 {
247 if (disk->flags & GENHD_FL_EXT_DEVT)
248 return DISK_MAX_PARTS;
249 return disk->minors;
250 }
251
disk_part_scan_enabled(struct gendisk * disk)252 static inline bool disk_part_scan_enabled(struct gendisk *disk)
253 {
254 return disk_max_parts(disk) > 1 &&
255 !(disk->flags & GENHD_FL_NO_PART_SCAN);
256 }
257
disk_devt(struct gendisk * disk)258 static inline dev_t disk_devt(struct gendisk *disk)
259 {
260 return MKDEV(disk->major, disk->first_minor);
261 }
262
part_devt(struct hd_struct * part)263 static inline dev_t part_devt(struct hd_struct *part)
264 {
265 return part_to_dev(part)->devt;
266 }
267
268 extern struct hd_struct *__disk_get_part(struct gendisk *disk, int partno);
269 extern struct hd_struct *disk_get_part(struct gendisk *disk, int partno);
270
disk_put_part(struct hd_struct * part)271 static inline void disk_put_part(struct hd_struct *part)
272 {
273 if (likely(part))
274 put_device(part_to_dev(part));
275 }
276
hd_sects_seq_init(struct hd_struct * p)277 static inline void hd_sects_seq_init(struct hd_struct *p)
278 {
279 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
280 seqcount_init(&p->nr_sects_seq);
281 #endif
282 }
283
284 /*
285 * Smarter partition iterator without context limits.
286 */
287 #define DISK_PITER_REVERSE (1 << 0) /* iterate in the reverse direction */
288 #define DISK_PITER_INCL_EMPTY (1 << 1) /* include 0-sized parts */
289 #define DISK_PITER_INCL_PART0 (1 << 2) /* include partition 0 */
290 #define DISK_PITER_INCL_EMPTY_PART0 (1 << 3) /* include empty partition 0 */
291
292 struct disk_part_iter {
293 struct gendisk *disk;
294 struct hd_struct *part;
295 int idx;
296 unsigned int flags;
297 };
298
299 extern void disk_part_iter_init(struct disk_part_iter *piter,
300 struct gendisk *disk, unsigned int flags);
301 extern struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter);
302 extern void disk_part_iter_exit(struct disk_part_iter *piter);
303 extern bool disk_has_partitions(struct gendisk *disk);
304
305 /* block/genhd.c */
306 extern void device_add_disk(struct device *parent, struct gendisk *disk,
307 const struct attribute_group **groups);
add_disk(struct gendisk * disk)308 static inline void add_disk(struct gendisk *disk)
309 {
310 device_add_disk(NULL, disk, NULL);
311 }
312 extern void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk);
add_disk_no_queue_reg(struct gendisk * disk)313 static inline void add_disk_no_queue_reg(struct gendisk *disk)
314 {
315 device_add_disk_no_queue_reg(NULL, disk);
316 }
317
318 extern void del_gendisk(struct gendisk *gp);
319 extern struct gendisk *get_gendisk(dev_t dev, int *partno);
320 extern struct block_device *bdget_disk(struct gendisk *disk, int partno);
321
322 extern void set_device_ro(struct block_device *bdev, int flag);
323 extern void set_disk_ro(struct gendisk *disk, int flag);
324
get_disk_ro(struct gendisk * disk)325 static inline int get_disk_ro(struct gendisk *disk)
326 {
327 return disk->part0.policy;
328 }
329
330 extern void disk_block_events(struct gendisk *disk);
331 extern void disk_unblock_events(struct gendisk *disk);
332 extern void disk_flush_events(struct gendisk *disk, unsigned int mask);
333 bool set_capacity_revalidate_and_notify(struct gendisk *disk, sector_t size,
334 bool update_bdev);
335
336 /* drivers/char/random.c */
337 extern void add_disk_randomness(struct gendisk *disk) __latent_entropy;
338 extern void rand_initialize_disk(struct gendisk *disk);
339
get_start_sect(struct block_device * bdev)340 static inline sector_t get_start_sect(struct block_device *bdev)
341 {
342 return bdev->bd_part->start_sect;
343 }
get_capacity(struct gendisk * disk)344 static inline sector_t get_capacity(struct gendisk *disk)
345 {
346 return disk->part0.nr_sects;
347 }
set_capacity(struct gendisk * disk,sector_t size)348 static inline void set_capacity(struct gendisk *disk, sector_t size)
349 {
350 disk->part0.nr_sects = size;
351 }
352
353 int bdev_disk_changed(struct block_device *bdev, bool invalidate);
354 int blk_add_partitions(struct gendisk *disk, struct block_device *bdev);
355 int blk_drop_partitions(struct block_device *bdev);
356
357 extern struct gendisk *__alloc_disk_node(int minors, int node_id);
358 extern struct kobject *get_disk_and_module(struct gendisk *disk);
359 extern void put_disk(struct gendisk *disk);
360 extern void put_disk_and_module(struct gendisk *disk);
361 extern void blk_register_region(dev_t devt, unsigned long range,
362 struct module *module,
363 struct kobject *(*probe)(dev_t, int *, void *),
364 int (*lock)(dev_t, void *),
365 void *data);
366 extern void blk_unregister_region(dev_t devt, unsigned long range);
367
368 #define alloc_disk_node(minors, node_id) \
369 ({ \
370 static struct lock_class_key __key; \
371 const char *__name; \
372 struct gendisk *__disk; \
373 \
374 __name = "(gendisk_completion)"#minors"("#node_id")"; \
375 \
376 __disk = __alloc_disk_node(minors, node_id); \
377 \
378 if (__disk) \
379 lockdep_init_map(&__disk->lockdep_map, __name, &__key, 0); \
380 \
381 __disk; \
382 })
383
384 #define alloc_disk(minors) alloc_disk_node(minors, NUMA_NO_NODE)
385
386 int register_blkdev(unsigned int major, const char *name);
387 void unregister_blkdev(unsigned int major, const char *name);
388
389 void revalidate_disk_size(struct gendisk *disk, bool verbose);
390 bool bdev_check_media_change(struct block_device *bdev);
391 int __invalidate_device(struct block_device *bdev, bool kill_dirty);
392 void bd_set_nr_sectors(struct block_device *bdev, sector_t sectors);
393
394 /* for drivers/char/raw.c: */
395 int blkdev_ioctl(struct block_device *, fmode_t, unsigned, unsigned long);
396 long compat_blkdev_ioctl(struct file *, unsigned, unsigned long);
397
398 #ifdef CONFIG_SYSFS
399 int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk);
400 void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk);
401 #else
bd_link_disk_holder(struct block_device * bdev,struct gendisk * disk)402 static inline int bd_link_disk_holder(struct block_device *bdev,
403 struct gendisk *disk)
404 {
405 return 0;
406 }
bd_unlink_disk_holder(struct block_device * bdev,struct gendisk * disk)407 static inline void bd_unlink_disk_holder(struct block_device *bdev,
408 struct gendisk *disk)
409 {
410 }
411 #endif /* CONFIG_SYSFS */
412
413 #ifdef CONFIG_BLOCK
414 void printk_all_partitions(void);
415 dev_t blk_lookup_devt(const char *name, int partno);
416 #else /* CONFIG_BLOCK */
printk_all_partitions(void)417 static inline void printk_all_partitions(void)
418 {
419 }
blk_lookup_devt(const char * name,int partno)420 static inline dev_t blk_lookup_devt(const char *name, int partno)
421 {
422 dev_t devt = MKDEV(0, 0);
423 return devt;
424 }
425 #endif /* CONFIG_BLOCK */
426
427 #endif /* _LINUX_GENHD_H */
428