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