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