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