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