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