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