OSDN Git Service

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