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