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