blob: 8b5330dd5ac094b68ffbf3922c7571e8f2998474 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/types.h>
Kay Sieversedfaa7c2007-05-21 22:08:01 +020014#include <linux/kdev_t.h>
Tejun Heoe71bf0d2008-09-03 09:03:02 +020015#include <linux/rcupdate.h>
Will Drewry6d1d8052010-08-31 15:47:05 -050016#include <linux/slab.h>
Ming Lei6c710132015-07-16 11:16:45 +080017#include <linux/percpu-refcount.h>
Andy Shevchenko63579782016-05-20 17:01:24 -070018#include <linux/uuid.h>
Michael Callahandbae2c52018-07-18 04:47:38 -070019#include <linux/blk_types.h>
Mikulas Patocka1226b8d2018-12-06 11:41:20 -050020#include <asm/local.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
David Howells93614012006-09-30 20:45:40 +020022#ifdef CONFIG_BLOCK
23
Tejun Heo548b10e2008-08-29 09:01:47 +020024#define dev_to_disk(device) container_of((device), struct gendisk, part0.__dev)
Tejun Heoed9e1982008-08-25 19:56:05 +090025#define dev_to_part(device) container_of((device), struct hd_struct, __dev)
Tejun Heo548b10e2008-08-29 09:01:47 +020026#define disk_to_dev(disk) (&(disk)->part0.__dev)
Tejun Heoed9e1982008-08-25 19:56:05 +090027#define part_to_dev(part) (&((part)->__dev))
Kay Sieversedfaa7c2007-05-21 22:08:01 +020028
Kay Sieversedfaa7c2007-05-21 22:08:01 +020029extern struct device_type part_type;
30extern struct kobject *block_depr;
31extern struct class block_class;
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033enum {
34/* These three have identical behaviour; use the second one if DOS FDISK gets
35 confused about extended/logical partitions starting past cylinder 1023. */
36 DOS_EXTENDED_PARTITION = 5,
37 LINUX_EXTENDED_PARTITION = 0x85,
38 WIN98_EXTENDED_PARTITION = 0x0f,
39
Fabio Massimo Di Nittod18d7682007-02-10 23:50:00 -080040 SUN_WHOLE_DISK = DOS_EXTENDED_PARTITION,
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 LINUX_SWAP_PARTITION = 0x82,
Olaf Hering4419d1a2007-02-10 01:45:47 -080043 LINUX_DATA_PARTITION = 0x83,
44 LINUX_LVM_PARTITION = 0x8e,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 LINUX_RAID_PARTITION = 0xfd, /* autodetect RAID partition */
46
47 SOLARIS_X86_PARTITION = LINUX_SWAP_PARTITION,
48 NEW_SOLARIS_X86_PARTITION = 0xbf,
49
50 DM6_AUX1PARTITION = 0x51, /* no DDO: use xlated geom */
51 DM6_AUX3PARTITION = 0x53, /* no DDO: use xlated geom */
52 DM6_PARTITION = 0x54, /* has DDO: use xlated geom & offset */
53 EZD_PARTITION = 0x55, /* EZ-DRIVE */
54
55 FREEBSD_PARTITION = 0xa5, /* FreeBSD Partition ID */
56 OPENBSD_PARTITION = 0xa6, /* OpenBSD Partition ID */
57 NETBSD_PARTITION = 0xa9, /* NetBSD Partition ID */
58 BSDI_PARTITION = 0xb7, /* BSDI Partition ID */
59 MINIX_PARTITION = 0x81, /* Minix Partition ID */
60 UNIXWARE_PARTITION = 0x63, /* Same as GNU_HURD and SCO Unix */
61};
62
Tejun Heo689d6fa2008-08-25 19:56:16 +090063#define DISK_MAX_PARTS 256
Tejun Heo3e1a7ff2008-08-25 19:56:17 +090064#define DISK_NAME_LEN 32
Tejun Heo689d6fa2008-08-25 19:56:16 +090065
David Woodhouse34186ef2006-04-25 14:07:57 +010066#include <linux/major.h>
67#include <linux/device.h>
68#include <linux/smp.h>
69#include <linux/string.h>
70#include <linux/fs.h>
Kristen Carlson Accardi8ce7ad7b2007-05-23 13:57:38 -070071#include <linux/workqueue.h>
David Woodhouse34186ef2006-04-25 14:07:57 +010072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073struct partition {
74 unsigned char boot_ind; /* 0x80 - active */
75 unsigned char head; /* starting head */
76 unsigned char sector; /* starting sector */
77 unsigned char cyl; /* starting cylinder */
78 unsigned char sys_ind; /* What partition type */
79 unsigned char end_head; /* end head */
80 unsigned char end_sector; /* end sector */
81 unsigned char end_cyl; /* end cylinder */
82 __le32 start_sect; /* starting sector counting from 0 */
83 __le32 nr_sects; /* nr of sectors in partition */
84} __attribute__((packed));
85
Jerome Marchandea5c48a2008-02-08 11:04:09 +010086struct disk_stats {
Omar Sandovalb57e99b2018-09-21 16:44:34 -070087 u64 nsecs[NR_STAT_GROUPS];
Michael Callahandbae2c52018-07-18 04:47:38 -070088 unsigned long sectors[NR_STAT_GROUPS];
89 unsigned long ios[NR_STAT_GROUPS];
90 unsigned long merges[NR_STAT_GROUPS];
Jerome Marchandea5c48a2008-02-08 11:04:09 +010091 unsigned long io_ticks;
92 unsigned long time_in_queue;
Mikulas Patocka1226b8d2018-12-06 11:41:20 -050093 local_t in_flight[2];
Jerome Marchandea5c48a2008-02-08 11:04:09 +010094};
Will Drewry6d1d8052010-08-31 15:47:05 -050095
96#define PARTITION_META_INFO_VOLNAMELTH 64
Stephen Warren1ad7e892012-11-08 16:12:25 -080097/*
98 * Enough for the string representation of any kind of UUID plus NULL.
99 * EFI UUID is 36 characters. MSDOS UUID is 11 characters.
100 */
Andy Shevchenko63579782016-05-20 17:01:24 -0700101#define PARTITION_META_INFO_UUIDLTH (UUID_STRING_LEN + 1)
Will Drewry6d1d8052010-08-31 15:47:05 -0500102
103struct partition_meta_info {
Stephen Warren1ad7e892012-11-08 16:12:25 -0800104 char uuid[PARTITION_META_INFO_UUIDLTH];
Will Drewry6d1d8052010-08-31 15:47:05 -0500105 u8 volname[PARTITION_META_INFO_VOLNAMELTH];
106};
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108struct hd_struct {
109 sector_t start_sect;
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200110 /*
111 * nr_sects is protected by sequence counter. One might extend a
112 * partition while IO is happening to it and update of nr_sects
113 * can be non-atomic on 32bit machines with 64bit sector_t.
114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 sector_t nr_sects;
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200116 seqcount_t nr_sects_seq;
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400117 sector_t alignment_offset;
Jens Axboea1706ac2011-05-30 07:42:51 +0200118 unsigned int discard_alignment;
Tejun Heoed9e1982008-08-25 19:56:05 +0900119 struct device __dev;
Jun'ichi Nomura6a4d44c2006-03-27 01:17:55 -0800120 struct kobject *holder_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 int policy, partno;
Will Drewry6d1d8052010-08-31 15:47:05 -0500122 struct partition_meta_info *info;
Akinobu Mitac17bb492006-12-08 02:39:46 -0800123#ifdef CONFIG_FAIL_MAKE_REQUEST
124 int make_it_fail;
125#endif
Jerome Marchandea5c48a2008-02-08 11:04:09 +0100126 unsigned long stamp;
Jerome Marchandea5c48a2008-02-08 11:04:09 +0100127#ifdef CONFIG_SMP
Tejun Heo43cf38e2010-02-02 14:38:57 +0900128 struct disk_stats __percpu *dkstats;
Jerome Marchandea5c48a2008-02-08 11:04:09 +0100129#else
130 struct disk_stats dkstats;
131#endif
Ming Lei6c710132015-07-16 11:16:45 +0800132 struct percpu_ref ref;
Yufen Yu94a2c3a2018-11-28 16:42:01 +0800133 struct rcu_work rcu_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
136#define GENHD_FL_REMOVABLE 1
NeilBrown97fedbb2010-03-16 08:55:32 +0100137/* 2 is unused */
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700138#define GENHD_FL_MEDIA_CHANGE_NOTIFY 4
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define GENHD_FL_CD 8
140#define GENHD_FL_UP 16
141#define GENHD_FL_SUPPRESS_PARTITION_INFO 32
Tejun Heo689d6fa2008-08-25 19:56:16 +0900142#define GENHD_FL_EXT_DEVT 64 /* allow extended devt */
Bartlomiej Zolnierkiewiczdb429e92009-06-07 13:52:52 +0200143#define GENHD_FL_NATIVE_CAPACITY 128
Tejun Heod4dc2102011-04-21 20:54:46 +0200144#define GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE 256
Tejun Heod27769e2011-08-23 20:01:04 +0200145#define GENHD_FL_NO_PART_SCAN 512
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300146#define GENHD_FL_HIDDEN 1024
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Tejun Heo77ea8872010-12-08 20:57:37 +0100148enum {
149 DISK_EVENT_MEDIA_CHANGE = 1 << 0, /* media changed */
150 DISK_EVENT_EJECT_REQUEST = 1 << 1, /* eject requested */
151};
152
Martin Wilckc92e2f02019-03-27 14:51:02 +0100153enum {
154 /* Poll even if events_poll_msecs is unset */
155 DISK_EVENT_FLAG_POLL = 1 << 0,
156 /* Forward events to udev */
157 DISK_EVENT_FLAG_UEVENT = 1 << 1,
158};
159
Tejun Heo540eed52008-08-25 19:56:15 +0900160struct disk_part_tbl {
161 struct rcu_head rcu_head;
162 int len;
Arnd Bergmann4d2deb42010-02-24 20:01:56 +0100163 struct hd_struct __rcu *last_lookup;
164 struct hd_struct __rcu *part[];
Tejun Heo540eed52008-08-25 19:56:15 +0900165};
166
Tejun Heo77ea8872010-12-08 20:57:37 +0100167struct disk_events;
Vishal Verma99e66082016-01-09 08:36:51 -0800168struct badblocks;
Tejun Heo77ea8872010-12-08 20:57:37 +0100169
Martin K. Petersen25520d52015-10-21 13:19:49 -0400170#if defined(CONFIG_BLK_DEV_INTEGRITY)
171
172struct blk_integrity {
Eric Biggers869ab902017-03-24 18:03:48 -0700173 const struct blk_integrity_profile *profile;
174 unsigned char flags;
175 unsigned char tuple_size;
176 unsigned char interval_exp;
177 unsigned char tag_size;
Martin K. Petersen25520d52015-10-21 13:19:49 -0400178};
179
180#endif /* CONFIG_BLK_DEV_INTEGRITY */
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182struct gendisk {
Tejun Heo689d6fa2008-08-25 19:56:16 +0900183 /* major, first_minor and minors are input parameters only,
184 * don't use directly. Use disk_devt() and disk_max_parts().
Tejun Heof331c022008-09-03 09:01:48 +0200185 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int major; /* major number of driver */
187 int first_minor;
188 int minors; /* maximum number of minors, =1 for
189 * disks that can't be partitioned. */
Tejun Heof331c022008-09-03 09:01:48 +0200190
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900191 char disk_name[DISK_NAME_LEN]; /* name of major driver */
Al Viro2c9ede52011-07-23 20:24:48 -0400192 char *(*devnode)(struct gendisk *gd, umode_t *mode);
Tejun Heo77ea8872010-12-08 20:57:37 +0100193
Martin Wilckc92e2f02019-03-27 14:51:02 +0100194 unsigned short events; /* supported events */
195 unsigned short event_flags; /* flags related to event processing */
Tejun Heo77ea8872010-12-08 20:57:37 +0100196
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200197 /* Array of pointers to partitions indexed by partno.
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200198 * Protected with matching bdev lock but stat and other
199 * non-critical accesses use RCU. Always access through
200 * helpers.
201 */
Arnd Bergmann4d2deb42010-02-24 20:01:56 +0100202 struct disk_part_tbl __rcu *part_tbl;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200203 struct hd_struct part0;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200204
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700205 const struct block_device_operations *fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 struct request_queue *queue;
207 void *private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 int flags;
Jan Kara56c09082018-02-26 13:01:41 +0100210 struct rw_semaphore lookup_sem;
Jun'ichi Nomura6a4d44c2006-03-27 01:17:55 -0800211 struct kobject *slave_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 struct timer_rand_state *random;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 atomic_t sync_io; /* RAID */
Tejun Heo77ea8872010-12-08 20:57:37 +0100215 struct disk_events *ev;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200216#ifdef CONFIG_BLK_DEV_INTEGRITY
Martin K. Petersenaff34e12015-10-21 13:19:27 -0400217 struct kobject integrity_kobj;
Martin K. Petersen25520d52015-10-21 13:19:49 -0400218#endif /* CONFIG_BLK_DEV_INTEGRITY */
Tejun Heo540eed52008-08-25 19:56:15 +0900219 int node_id;
Vishal Verma99e66082016-01-09 08:36:51 -0800220 struct badblocks *bb;
Byungchul Parke319e1f2017-10-25 17:56:05 +0900221 struct lockdep_map lockdep_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222};
223
Tejun Heo310a2c12008-08-25 19:47:17 +0900224static inline struct gendisk *part_to_disk(struct hd_struct *part)
225{
Tejun Heo548b10e2008-08-29 09:01:47 +0200226 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 }
Tejun Heo310a2c12008-08-25 19:47:17 +0900232 return NULL;
233}
234
Tejun Heof331c022008-09-03 09:01:48 +0200235static inline int disk_max_parts(struct gendisk *disk)
236{
Tejun Heo689d6fa2008-08-25 19:56:16 +0900237 if (disk->flags & GENHD_FL_EXT_DEVT)
238 return DISK_MAX_PARTS;
239 return disk->minors;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200240}
241
Tejun Heod27769e2011-08-23 20:01:04 +0200242static inline bool disk_part_scan_enabled(struct gendisk *disk)
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200243{
Tejun Heod27769e2011-08-23 20:01:04 +0200244 return disk_max_parts(disk) > 1 &&
245 !(disk->flags & GENHD_FL_NO_PART_SCAN);
Tejun Heof331c022008-09-03 09:01:48 +0200246}
247
248static inline dev_t disk_devt(struct gendisk *disk)
249{
Christoph Hellwig517bf3c2017-11-02 21:29:52 +0300250 return MKDEV(disk->major, disk->first_minor);
Tejun Heof331c022008-09-03 09:01:48 +0200251}
252
253static inline dev_t part_devt(struct hd_struct *part)
254{
Tejun Heoed9e1982008-08-25 19:56:05 +0900255 return part_to_dev(part)->devt;
Tejun Heof331c022008-09-03 09:01:48 +0200256}
257
Greg Edwards67f25192017-10-24 11:21:48 -0600258extern struct hd_struct *__disk_get_part(struct gendisk *disk, int partno);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200259extern struct hd_struct *disk_get_part(struct gendisk *disk, int partno);
260
261static inline void disk_put_part(struct hd_struct *part)
262{
263 if (likely(part))
Tejun Heoed9e1982008-08-25 19:56:05 +0900264 put_device(part_to_dev(part));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200265}
266
267/*
268 * Smarter partition iterator without context limits.
269 */
270#define DISK_PITER_REVERSE (1 << 0) /* iterate in the reverse direction */
271#define DISK_PITER_INCL_EMPTY (1 << 1) /* include 0-sized parts */
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200272#define DISK_PITER_INCL_PART0 (1 << 2) /* include partition 0 */
Tejun Heo71982a42009-04-17 08:34:48 +0200273#define DISK_PITER_INCL_EMPTY_PART0 (1 << 3) /* include empty partition 0 */
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200274
275struct disk_part_iter {
276 struct gendisk *disk;
277 struct hd_struct *part;
278 int idx;
279 unsigned int flags;
280};
281
282extern void disk_part_iter_init(struct disk_part_iter *piter,
283 struct gendisk *disk, unsigned int flags);
284extern struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter);
285extern void disk_part_iter_exit(struct disk_part_iter *piter);
286
287extern struct hd_struct *disk_map_sector_rcu(struct gendisk *disk,
288 sector_t sector);
289
Tejun Heoc9959052008-08-25 19:47:21 +0900290/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 * Macros to operate on percpu disk statistics:
292 *
Tejun Heoc9959052008-08-25 19:47:21 +0900293 * {disk|part|all}_stat_{add|sub|inc|dec}() modify the stat counters
294 * and should be called between disk_stat_lock() and
295 * disk_stat_unlock().
296 *
297 * part_stat_read() can be called at any time.
298 *
299 * part_stat_{add|set_all}() and {init|free}_part_stats are for
300 * internal use only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 */
302#ifdef CONFIG_SMP
Tejun Heo074a7ac2008-08-25 19:56:14 +0900303#define part_stat_lock() ({ rcu_read_lock(); get_cpu(); })
304#define part_stat_unlock() do { put_cpu(); rcu_read_unlock(); } while (0)
Tejun Heoc9959052008-08-25 19:47:21 +0900305
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500306#define part_stat_get_cpu(part, field, cpu) \
307 (per_cpu_ptr((part)->dkstats, (cpu))->field)
308
309#define part_stat_get(part, field) \
310 part_stat_get_cpu(part, field, smp_processor_id())
Jerome Marchandea5c48a2008-02-08 11:04:09 +0100311
312#define part_stat_read(part, field) \
313({ \
Tejun Heo074a7ac2008-08-25 19:56:14 +0900314 typeof((part)->dkstats->field) res = 0; \
Stephen Hemminger7af92f82010-01-06 15:45:55 -0800315 unsigned int _cpu; \
316 for_each_possible_cpu(_cpu) \
317 res += per_cpu_ptr((part)->dkstats, _cpu)->field; \
Jerome Marchandea5c48a2008-02-08 11:04:09 +0100318 res; \
319})
320
Jens Axboe28f13702008-05-07 10:15:46 +0200321static inline void part_stat_set_all(struct hd_struct *part, int value)
322{
Jerome Marchandea5c48a2008-02-08 11:04:09 +0100323 int i;
Jens Axboe28f13702008-05-07 10:15:46 +0200324
Jerome Marchandea5c48a2008-02-08 11:04:09 +0100325 for_each_possible_cpu(i)
326 memset(per_cpu_ptr(part->dkstats, i), value,
Jens Axboe28f13702008-05-07 10:15:46 +0200327 sizeof(struct disk_stats));
Jerome Marchandea5c48a2008-02-08 11:04:09 +0100328}
Tejun Heoc9959052008-08-25 19:47:21 +0900329
Jerome Marchandea5c48a2008-02-08 11:04:09 +0100330static inline int init_part_stats(struct hd_struct *part)
331{
332 part->dkstats = alloc_percpu(struct disk_stats);
333 if (!part->dkstats)
334 return 0;
335 return 1;
336}
337
338static inline void free_part_stats(struct hd_struct *part)
339{
340 free_percpu(part->dkstats);
341}
342
Tejun Heo074a7ac2008-08-25 19:56:14 +0900343#else /* !CONFIG_SMP */
344#define part_stat_lock() ({ rcu_read_lock(); 0; })
345#define part_stat_unlock() rcu_read_unlock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500347#define part_stat_get(part, field) ((part)->dkstats.field)
348#define part_stat_get_cpu(part, field, cpu) part_stat_get(part, field)
349#define part_stat_read(part, field) part_stat_get(part, field)
Tejun Heo074a7ac2008-08-25 19:56:14 +0900350
351static inline void part_stat_set_all(struct hd_struct *part, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Tejun Heo074a7ac2008-08-25 19:56:14 +0900353 memset(&part->dkstats, value, sizeof(struct disk_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
Jerome Marchandea5c48a2008-02-08 11:04:09 +0100355
356static inline int init_part_stats(struct hd_struct *part)
357{
358 return 1;
359}
360
361static inline void free_part_stats(struct hd_struct *part)
362{
363}
Tejun Heo074a7ac2008-08-25 19:56:14 +0900364
365#endif /* CONFIG_SMP */
366
Omar Sandovalb57e99b2018-09-21 16:44:34 -0700367#define part_stat_read_msecs(part, which) \
368 div_u64(part_stat_read(part, nsecs[which]), NSEC_PER_MSEC)
369
Michael Callahan59767fb2018-07-18 04:47:37 -0700370#define part_stat_read_accum(part, field) \
Michael Callahandbae2c52018-07-18 04:47:38 -0700371 (part_stat_read(part, field[STAT_READ]) + \
Michael Callahanbdca3c82018-07-18 04:47:40 -0700372 part_stat_read(part, field[STAT_WRITE]) + \
373 part_stat_read(part, field[STAT_DISCARD]))
Michael Callahan59767fb2018-07-18 04:47:37 -0700374
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500375#define __part_stat_add(part, field, addnd) \
376 (part_stat_get(part, field) += (addnd))
377
Mike Snitzer112f1582018-12-06 11:41:18 -0500378#define part_stat_add(part, field, addnd) do { \
379 __part_stat_add((part), field, addnd); \
Tejun Heo074a7ac2008-08-25 19:56:14 +0900380 if ((part)->partno) \
Mike Snitzer112f1582018-12-06 11:41:18 -0500381 __part_stat_add(&part_to_disk((part))->part0, \
Tejun Heo074a7ac2008-08-25 19:56:14 +0900382 field, addnd); \
383} while (0)
384
Mike Snitzer112f1582018-12-06 11:41:18 -0500385#define part_stat_dec(gendiskp, field) \
386 part_stat_add(gendiskp, field, -1)
387#define part_stat_inc(gendiskp, field) \
388 part_stat_add(gendiskp, field, 1)
389#define part_stat_sub(gendiskp, field, subnd) \
390 part_stat_add(gendiskp, field, -subnd)
Tejun Heo074a7ac2008-08-25 19:56:14 +0900391
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500392#define part_stat_local_dec(gendiskp, field) \
393 local_dec(&(part_stat_get(gendiskp, field)))
394#define part_stat_local_inc(gendiskp, field) \
395 local_inc(&(part_stat_get(gendiskp, field)))
396#define part_stat_local_read(gendiskp, field) \
397 local_read(&(part_stat_get(gendiskp, field)))
398#define part_stat_local_read_cpu(gendiskp, field, cpu) \
399 local_read(&(part_stat_get_cpu(gendiskp, field, cpu)))
400
Mikulas Patockae016b782018-12-06 11:41:21 -0500401unsigned int part_in_flight(struct request_queue *q, struct hd_struct *part);
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700402void part_in_flight_rw(struct request_queue *q, struct hd_struct *part,
403 unsigned int inflight[2]);
Jens Axboef299b7c2017-08-08 17:51:45 -0600404void part_dec_in_flight(struct request_queue *q, struct hd_struct *part,
405 int rw);
406void part_inc_in_flight(struct request_queue *q, struct hd_struct *part,
407 int rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Will Drewry6d1d8052010-08-31 15:47:05 -0500409static inline struct partition_meta_info *alloc_part_info(struct gendisk *disk)
410{
411 if (disk)
412 return kzalloc_node(sizeof(struct partition_meta_info),
413 GFP_KERNEL, disk->node_id);
414 return kzalloc(sizeof(struct partition_meta_info), GFP_KERNEL);
415}
416
417static inline void free_part_info(struct hd_struct *part)
418{
419 kfree(part->info);
420}
421
Mikulas Patocka5b18b5a2018-12-06 11:41:19 -0500422void update_io_ticks(struct hd_struct *part, unsigned long now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Petros Koutoupis32ca1632009-03-10 08:25:54 +0100424/* block/genhd.c */
Hannes Reineckefef912b2018-09-28 08:17:19 +0200425extern void device_add_disk(struct device *parent, struct gendisk *disk,
426 const struct attribute_group **groups);
Dan Williamse63a46b2016-06-15 18:17:27 -0700427static inline void add_disk(struct gendisk *disk)
428{
Hannes Reineckefef912b2018-09-28 08:17:19 +0200429 device_add_disk(NULL, disk, NULL);
Dan Williamse63a46b2016-06-15 18:17:27 -0700430}
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500431extern void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk);
432static inline void add_disk_no_queue_reg(struct gendisk *disk)
433{
434 device_add_disk_no_queue_reg(NULL, disk);
435}
Dan Williamse63a46b2016-06-15 18:17:27 -0700436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437extern void del_gendisk(struct gendisk *gp);
Tejun Heocf771cb2008-09-03 09:01:09 +0200438extern struct gendisk *get_gendisk(dev_t dev, int *partno);
Tejun Heof331c022008-09-03 09:01:48 +0200439extern struct block_device *bdget_disk(struct gendisk *disk, int partno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441extern void set_device_ro(struct block_device *bdev, int flag);
442extern void set_disk_ro(struct gendisk *disk, int flag);
443
Tejun Heob7db9952008-08-25 19:56:10 +0900444static inline int get_disk_ro(struct gendisk *disk)
445{
446 return disk->part0.policy;
447}
448
Tejun Heo77ea8872010-12-08 20:57:37 +0100449extern void disk_block_events(struct gendisk *disk);
450extern void disk_unblock_events(struct gendisk *disk);
Tejun Heo85ef06d2011-07-01 16:17:47 +0200451extern void disk_flush_events(struct gendisk *disk, unsigned int mask);
Tejun Heo77ea8872010-12-08 20:57:37 +0100452extern unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask);
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/* drivers/char/random.c */
Emese Revfy0766f782016-06-20 20:42:34 +0200455extern void add_disk_randomness(struct gendisk *disk) __latent_entropy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456extern void rand_initialize_disk(struct gendisk *disk);
457
458static inline sector_t get_start_sect(struct block_device *bdev)
459{
Tejun Heo0762b8b2008-08-25 19:56:12 +0900460 return bdev->bd_part->start_sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462static inline sector_t get_capacity(struct gendisk *disk)
463{
Tejun Heo80795ae2008-08-25 19:56:07 +0900464 return disk->part0.nr_sects;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466static inline void set_capacity(struct gendisk *disk, sector_t size)
467{
Tejun Heo80795ae2008-08-25 19:56:07 +0900468 disk->part0.nr_sects = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471#ifdef CONFIG_SOLARIS_X86_PARTITION
472
Mark Fortescueb84d8792007-07-25 18:30:08 -0700473#define SOLARIS_X86_NUMSLICE 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474#define SOLARIS_X86_VTOC_SANE (0x600DDEEEUL)
475
476struct solaris_x86_slice {
477 __le16 s_tag; /* ID tag of partition */
478 __le16 s_flag; /* permission flags */
479 __le32 s_start; /* start sector no of partition */
480 __le32 s_size; /* # of blocks in partition */
481};
482
483struct solaris_x86_vtoc {
484 unsigned int v_bootinfo[3]; /* info needed by mboot (unsupported) */
485 __le32 v_sanity; /* to verify vtoc sanity */
486 __le32 v_version; /* layout version */
487 char v_volume[8]; /* volume name */
488 __le16 v_sectorsz; /* sector size in bytes */
489 __le16 v_nparts; /* number of partitions */
490 unsigned int v_reserved[10]; /* free space */
491 struct solaris_x86_slice
492 v_slice[SOLARIS_X86_NUMSLICE]; /* slice headers */
493 unsigned int timestamp[SOLARIS_X86_NUMSLICE]; /* timestamp (unsupported) */
494 char v_asciilabel[128]; /* for compatibility */
495};
496
497#endif /* CONFIG_SOLARIS_X86_PARTITION */
498
499#ifdef CONFIG_BSD_DISKLABEL
500/*
501 * BSD disklabel support by Yossi Gottlieb <yogo@math.tau.ac.il>
502 * updated by Marc Espie <Marc.Espie@openbsd.org>
503 */
504
505/* check against BSD src/sys/sys/disklabel.h for consistency */
506
507#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
508#define BSD_MAXPARTITIONS 16
509#define OPENBSD_MAXPARTITIONS 16
510#define BSD_FS_UNUSED 0 /* disklabel unused partition entry ID */
511struct bsd_disklabel {
512 __le32 d_magic; /* the magic number */
513 __s16 d_type; /* drive type */
514 __s16 d_subtype; /* controller/d_type specific */
515 char d_typename[16]; /* type name, e.g. "eagle" */
516 char d_packname[16]; /* pack identifier */
517 __u32 d_secsize; /* # of bytes per sector */
518 __u32 d_nsectors; /* # of data sectors per track */
519 __u32 d_ntracks; /* # of tracks per cylinder */
520 __u32 d_ncylinders; /* # of data cylinders per unit */
521 __u32 d_secpercyl; /* # of data sectors per cylinder */
522 __u32 d_secperunit; /* # of data sectors per unit */
523 __u16 d_sparespertrack; /* # of spare sectors per track */
524 __u16 d_sparespercyl; /* # of spare sectors per cylinder */
525 __u32 d_acylinders; /* # of alt. cylinders per unit */
526 __u16 d_rpm; /* rotational speed */
527 __u16 d_interleave; /* hardware sector interleave */
528 __u16 d_trackskew; /* sector 0 skew, per track */
529 __u16 d_cylskew; /* sector 0 skew, per cylinder */
530 __u32 d_headswitch; /* head switch time, usec */
531 __u32 d_trkseek; /* track-to-track seek, usec */
532 __u32 d_flags; /* generic flags */
533#define NDDATA 5
534 __u32 d_drivedata[NDDATA]; /* drive-type specific information */
535#define NSPARE 5
536 __u32 d_spare[NSPARE]; /* reserved for future use */
537 __le32 d_magic2; /* the magic number (again) */
538 __le16 d_checksum; /* xor of data incl. partitions */
539
540 /* filesystem and partition information: */
541 __le16 d_npartitions; /* number of partitions in following */
542 __le32 d_bbsize; /* size of boot area at sn0, bytes */
543 __le32 d_sbsize; /* max size of fs superblock, bytes */
544 struct bsd_partition { /* the partition table */
545 __le32 p_size; /* number of sectors in partition */
546 __le32 p_offset; /* starting sector */
547 __le32 p_fsize; /* filesystem basic fragment size */
548 __u8 p_fstype; /* filesystem type, see below */
549 __u8 p_frag; /* filesystem fragments per block */
550 __le16 p_cpg; /* filesystem cylinders per group */
551 } d_partitions[BSD_MAXPARTITIONS]; /* actually may be more */
552};
553
554#endif /* CONFIG_BSD_DISKLABEL */
555
556#ifdef CONFIG_UNIXWARE_DISKLABEL
557/*
558 * Unixware slices support by Andrzej Krzysztofowicz <ankry@mif.pg.gda.pl>
559 * and Krzysztof G. Baranowski <kgb@knm.org.pl>
560 */
561
562#define UNIXWARE_DISKMAGIC (0xCA5E600DUL) /* The disk magic number */
563#define UNIXWARE_DISKMAGIC2 (0x600DDEEEUL) /* The slice table magic nr */
564#define UNIXWARE_NUMSLICE 16
565#define UNIXWARE_FS_UNUSED 0 /* Unused slice entry ID */
566
567struct unixware_slice {
568 __le16 s_label; /* label */
569 __le16 s_flags; /* permission flags */
570 __le32 start_sect; /* starting sector */
571 __le32 nr_sects; /* number of sectors in slice */
572};
573
574struct unixware_disklabel {
575 __le32 d_type; /* drive type */
576 __le32 d_magic; /* the magic number */
577 __le32 d_version; /* version number */
578 char d_serial[12]; /* serial number of the device */
579 __le32 d_ncylinders; /* # of data cylinders per device */
580 __le32 d_ntracks; /* # of tracks per cylinder */
581 __le32 d_nsectors; /* # of data sectors per track */
582 __le32 d_secsize; /* # of bytes per sector */
583 __le32 d_part_start; /* # of first sector of this partition */
584 __le32 d_unknown1[12]; /* ? */
585 __le32 d_alt_tbl; /* byte offset of alternate table */
586 __le32 d_alt_len; /* byte length of alternate table */
587 __le32 d_phys_cyl; /* # of physical cylinders per device */
588 __le32 d_phys_trk; /* # of physical tracks per cylinder */
589 __le32 d_phys_sec; /* # of physical sectors per track */
590 __le32 d_phys_bytes; /* # of physical bytes per sector */
591 __le32 d_unknown2; /* ? */
592 __le32 d_unknown3; /* ? */
593 __le32 d_pad[8]; /* pad */
594
595 struct unixware_vtoc {
596 __le32 v_magic; /* the magic number */
597 __le32 v_version; /* version number */
598 char v_name[8]; /* volume name */
599 __le16 v_nslices; /* # of slices */
600 __le16 v_unknown1; /* ? */
601 __le32 v_reserved[10]; /* reserved */
602 struct unixware_slice
603 v_slice[UNIXWARE_NUMSLICE]; /* slice headers */
604 } vtoc;
605
606}; /* 408 */
607
608#endif /* CONFIG_UNIXWARE_DISKLABEL */
609
610#ifdef CONFIG_MINIX_SUBPARTITION
611# define MINIX_NR_SUBPARTITIONS 4
612#endif /* CONFIG_MINIX_SUBPARTITION */
613
Fabio Massimo Di Nittod18d7682007-02-10 23:50:00 -0800614#define ADDPART_FLAG_NONE 0
615#define ADDPART_FLAG_RAID 1
616#define ADDPART_FLAG_WHOLEDISK 2
617
Tejun Heobcce3de2008-08-25 19:47:22 +0900618extern int blk_alloc_devt(struct hd_struct *part, dev_t *devt);
619extern void blk_free_devt(dev_t devt);
Yufen Yu6fcc44d2019-04-02 20:06:34 +0800620extern void blk_invalidate_devt(dev_t devt);
Tejun Heocf771cb2008-09-03 09:01:09 +0200621extern dev_t blk_lookup_devt(const char *name, int partno);
622extern char *disk_name (struct gendisk *hd, int partno, char *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Tejun Heo540eed52008-08-25 19:56:15 +0900624extern int disk_expand_part_tbl(struct gendisk *disk, int target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625extern int rescan_partitions(struct gendisk *disk, struct block_device *bdev);
Jun'ichi Nomurafe316bf2012-03-02 10:38:33 +0100626extern int invalidate_partitions(struct gendisk *disk, struct block_device *bdev);
Tejun Heoba329292008-11-10 15:29:58 +0900627extern struct hd_struct * __must_check add_partition(struct gendisk *disk,
628 int partno, sector_t start,
Will Drewry6d1d8052010-08-31 15:47:05 -0500629 sector_t len, int flags,
630 struct partition_meta_info
631 *info);
Ming Lei6c710132015-07-16 11:16:45 +0800632extern void __delete_partition(struct percpu_ref *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633extern void delete_partition(struct gendisk *, int);
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700634extern void printk_all_partitions(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Byungchul Parke319e1f2017-10-25 17:56:05 +0900636extern struct gendisk *__alloc_disk_node(int minors, int node_id);
Jan Kara3079c222018-02-26 13:01:38 +0100637extern struct kobject *get_disk_and_module(struct gendisk *disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638extern void put_disk(struct gendisk *disk);
Jan Kara9df6c292018-02-26 13:01:39 +0100639extern void put_disk_and_module(struct gendisk *disk);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200640extern void blk_register_region(dev_t devt, unsigned long range,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct module *module,
642 struct kobject *(*probe)(dev_t, int *, void *),
643 int (*lock)(dev_t, void *),
644 void *data);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200645extern void blk_unregister_region(dev_t devt, unsigned long range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Tejun Heoe5610522008-08-25 19:56:09 +0900647extern ssize_t part_size_show(struct device *dev,
648 struct device_attribute *attr, char *buf);
Tejun Heo074a7ac2008-08-25 19:56:14 +0900649extern ssize_t part_stat_show(struct device *dev,
650 struct device_attribute *attr, char *buf);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200651extern ssize_t part_inflight_show(struct device *dev,
652 struct device_attribute *attr, char *buf);
Tejun Heoeddb2e22008-08-25 19:56:13 +0900653#ifdef CONFIG_FAIL_MAKE_REQUEST
654extern ssize_t part_fail_show(struct device *dev,
655 struct device_attribute *attr, char *buf);
656extern ssize_t part_fail_store(struct device *dev,
657 struct device_attribute *attr,
658 const char *buf, size_t count);
659#endif /* CONFIG_FAIL_MAKE_REQUEST */
Tejun Heoe5610522008-08-25 19:56:09 +0900660
Byungchul Parke319e1f2017-10-25 17:56:05 +0900661#define alloc_disk_node(minors, node_id) \
662({ \
663 static struct lock_class_key __key; \
664 const char *__name; \
665 struct gendisk *__disk; \
666 \
667 __name = "(gendisk_completion)"#minors"("#node_id")"; \
668 \
669 __disk = __alloc_disk_node(minors, node_id); \
670 \
671 if (__disk) \
672 lockdep_init_map(&__disk->lockdep_map, __name, &__key, 0); \
673 \
674 __disk; \
675})
676
677#define alloc_disk(minors) alloc_disk_node(minors, NUMA_NO_NODE)
678
Ming Lei6c710132015-07-16 11:16:45 +0800679static inline int hd_ref_init(struct hd_struct *part)
Jens Axboe6c23a962011-01-07 08:43:37 +0100680{
Ming Lei6c710132015-07-16 11:16:45 +0800681 if (percpu_ref_init(&part->ref, __delete_partition, 0,
682 GFP_KERNEL))
683 return -ENOMEM;
684 return 0;
Jens Axboe6c23a962011-01-07 08:43:37 +0100685}
686
687static inline void hd_struct_get(struct hd_struct *part)
688{
Ming Lei6c710132015-07-16 11:16:45 +0800689 percpu_ref_get(&part->ref);
Jens Axboe6c23a962011-01-07 08:43:37 +0100690}
691
692static inline int hd_struct_try_get(struct hd_struct *part)
693{
Ming Lei6c710132015-07-16 11:16:45 +0800694 return percpu_ref_tryget_live(&part->ref);
Jens Axboe6c23a962011-01-07 08:43:37 +0100695}
696
697static inline void hd_struct_put(struct hd_struct *part)
698{
Ming Lei6c710132015-07-16 11:16:45 +0800699 percpu_ref_put(&part->ref);
700}
701
702static inline void hd_struct_kill(struct hd_struct *part)
703{
704 percpu_ref_kill(&part->ref);
Jens Axboe6c23a962011-01-07 08:43:37 +0100705}
706
Ming Leib54e5ed2015-07-16 11:16:44 +0800707static inline void hd_free_part(struct hd_struct *part)
708{
709 free_part_stats(part);
710 free_part_info(part);
Ming Lei6c710132015-07-16 11:16:45 +0800711 percpu_ref_exit(&part->ref);
Ming Leib54e5ed2015-07-16 11:16:44 +0800712}
713
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200714/*
715 * Any access of part->nr_sects which is not protected by partition
716 * bd_mutex or gendisk bdev bd_mutex, should be done using this
717 * accessor function.
718 *
719 * Code written along the lines of i_size_read() and i_size_write().
720 * CONFIG_PREEMPT case optimizes the case of UP kernel with preemption
721 * on.
722 */
723static inline sector_t part_nr_sects_read(struct hd_struct *part)
724{
Christoph Hellwig72deb452019-04-05 18:08:59 +0200725#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200726 sector_t nr_sects;
727 unsigned seq;
728 do {
729 seq = read_seqcount_begin(&part->nr_sects_seq);
730 nr_sects = part->nr_sects;
731 } while (read_seqcount_retry(&part->nr_sects_seq, seq));
732 return nr_sects;
Christoph Hellwig72deb452019-04-05 18:08:59 +0200733#elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPT)
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200734 sector_t nr_sects;
735
736 preempt_disable();
737 nr_sects = part->nr_sects;
738 preempt_enable();
739 return nr_sects;
740#else
741 return part->nr_sects;
742#endif
743}
744
745/*
746 * Should be called with mutex lock held (typically bd_mutex) of partition
747 * to provide mutual exlusion among writers otherwise seqcount might be
748 * left in wrong state leaving the readers spinning infinitely.
749 */
750static inline void part_nr_sects_write(struct hd_struct *part, sector_t size)
751{
Christoph Hellwig72deb452019-04-05 18:08:59 +0200752#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200753 write_seqcount_begin(&part->nr_sects_seq);
754 part->nr_sects = size;
755 write_seqcount_end(&part->nr_sects_seq);
Christoph Hellwig72deb452019-04-05 18:08:59 +0200756#elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPT)
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200757 preempt_disable();
758 part->nr_sects = size;
759 preempt_enable();
760#else
761 part->nr_sects = size;
762#endif
763}
764
Martin K. Petersen25520d52015-10-21 13:19:49 -0400765#if defined(CONFIG_BLK_DEV_INTEGRITY)
766extern void blk_integrity_add(struct gendisk *);
767extern void blk_integrity_del(struct gendisk *);
Martin K. Petersen25520d52015-10-21 13:19:49 -0400768#else /* CONFIG_BLK_DEV_INTEGRITY */
769static inline void blk_integrity_add(struct gendisk *disk) { }
770static inline void blk_integrity_del(struct gendisk *disk) { }
Martin K. Petersen25520d52015-10-21 13:19:49 -0400771#endif /* CONFIG_BLK_DEV_INTEGRITY */
772
Jens Axboe87c1efb2007-05-11 13:29:54 +0200773#else /* CONFIG_BLOCK */
774
775static inline void printk_all_partitions(void) { }
776
Tejun Heocf771cb2008-09-03 09:01:09 +0200777static inline dev_t blk_lookup_devt(const char *name, int partno)
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200778{
779 dev_t devt = MKDEV(0, 0);
780 return devt;
781}
Jens Axboe87c1efb2007-05-11 13:29:54 +0200782#endif /* CONFIG_BLOCK */
David Howells93614012006-09-30 20:45:40 +0200783
David Woodhousea8ae50b2008-03-12 17:52:56 +0100784#endif /* _LINUX_GENHD_H */