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