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