blob: 0b820ff058393760767b7406a7d19296ead2dfce [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
David Howells93614012006-09-30 20:45:40 +020021#ifdef CONFIG_BLOCK
22
Tejun Heo548b10e2008-08-29 09:01:47 +020023#define dev_to_disk(device) container_of((device), struct gendisk, part0.__dev)
Tejun Heoed9e1982008-08-25 19:56:05 +090024#define dev_to_part(device) container_of((device), struct hd_struct, __dev)
Tejun Heo548b10e2008-08-29 09:01:47 +020025#define disk_to_dev(disk) (&(disk)->part0.__dev)
Tejun Heoed9e1982008-08-25 19:56:05 +090026#define part_to_dev(part) (&((part)->__dev))
Kay Sieversedfaa7c2007-05-21 22:08:01 +020027
Kay Sieversedfaa7c2007-05-21 22:08:01 +020028extern struct device_type part_type;
29extern struct kobject *block_depr;
30extern struct class block_class;
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032enum {
33/* These three have identical behaviour; use the second one if DOS FDISK gets
34 confused about extended/logical partitions starting past cylinder 1023. */
35 DOS_EXTENDED_PARTITION = 5,
36 LINUX_EXTENDED_PARTITION = 0x85,
37 WIN98_EXTENDED_PARTITION = 0x0f,
38
Fabio Massimo Di Nittod18d7682007-02-10 23:50:00 -080039 SUN_WHOLE_DISK = DOS_EXTENDED_PARTITION,
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 LINUX_SWAP_PARTITION = 0x82,
Olaf Hering4419d1a2007-02-10 01:45:47 -080042 LINUX_DATA_PARTITION = 0x83,
43 LINUX_LVM_PARTITION = 0x8e,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 LINUX_RAID_PARTITION = 0xfd, /* autodetect RAID partition */
45
46 SOLARIS_X86_PARTITION = LINUX_SWAP_PARTITION,
47 NEW_SOLARIS_X86_PARTITION = 0xbf,
48
49 DM6_AUX1PARTITION = 0x51, /* no DDO: use xlated geom */
50 DM6_AUX3PARTITION = 0x53, /* no DDO: use xlated geom */
51 DM6_PARTITION = 0x54, /* has DDO: use xlated geom & offset */
52 EZD_PARTITION = 0x55, /* EZ-DRIVE */
53
54 FREEBSD_PARTITION = 0xa5, /* FreeBSD Partition ID */
55 OPENBSD_PARTITION = 0xa6, /* OpenBSD Partition ID */
56 NETBSD_PARTITION = 0xa9, /* NetBSD Partition ID */
57 BSDI_PARTITION = 0xb7, /* BSDI Partition ID */
58 MINIX_PARTITION = 0x81, /* Minix Partition ID */
59 UNIXWARE_PARTITION = 0x63, /* Same as GNU_HURD and SCO Unix */
60};
61
Tejun Heo689d6fa2008-08-25 19:56:16 +090062#define DISK_MAX_PARTS 256
Tejun Heo3e1a7ff2008-08-25 19:56:17 +090063#define DISK_NAME_LEN 32
Tejun Heo689d6fa2008-08-25 19:56:16 +090064
David Woodhouse34186ef2006-04-25 14:07:57 +010065#include <linux/major.h>
66#include <linux/device.h>
67#include <linux/smp.h>
68#include <linux/string.h>
69#include <linux/fs.h>
Kristen Carlson Accardi8ce7ad7b2007-05-23 13:57:38 -070070#include <linux/workqueue.h>
David Woodhouse34186ef2006-04-25 14:07:57 +010071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072struct partition {
73 unsigned char boot_ind; /* 0x80 - active */
74 unsigned char head; /* starting head */
75 unsigned char sector; /* starting sector */
76 unsigned char cyl; /* starting cylinder */
77 unsigned char sys_ind; /* What partition type */
78 unsigned char end_head; /* end head */
79 unsigned char end_sector; /* end sector */
80 unsigned char end_cyl; /* end cylinder */
81 __le32 start_sect; /* starting sector counting from 0 */
82 __le32 nr_sects; /* nr of sectors in partition */
83} __attribute__((packed));
84
Jerome Marchandea5c48a2008-02-08 11:04:09 +010085struct disk_stats {
Michael Callahandbae2c52018-07-18 04:47:38 -070086 unsigned long sectors[NR_STAT_GROUPS];
87 unsigned long ios[NR_STAT_GROUPS];
88 unsigned long merges[NR_STAT_GROUPS];
89 unsigned long ticks[NR_STAT_GROUPS];
Jerome Marchandea5c48a2008-02-08 11:04:09 +010090 unsigned long io_ticks;
91 unsigned long time_in_queue;
92};
Will Drewry6d1d8052010-08-31 15:47:05 -050093
94#define PARTITION_META_INFO_VOLNAMELTH 64
Stephen Warren1ad7e892012-11-08 16:12:25 -080095/*
96 * Enough for the string representation of any kind of UUID plus NULL.
97 * EFI UUID is 36 characters. MSDOS UUID is 11 characters.
98 */
Andy Shevchenko63579782016-05-20 17:01:24 -070099#define PARTITION_META_INFO_UUIDLTH (UUID_STRING_LEN + 1)
Will Drewry6d1d8052010-08-31 15:47:05 -0500100
101struct partition_meta_info {
Stephen Warren1ad7e892012-11-08 16:12:25 -0800102 char uuid[PARTITION_META_INFO_UUIDLTH];
Will Drewry6d1d8052010-08-31 15:47:05 -0500103 u8 volname[PARTITION_META_INFO_VOLNAMELTH];
104};
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106struct hd_struct {
107 sector_t start_sect;
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200108 /*
109 * nr_sects is protected by sequence counter. One might extend a
110 * partition while IO is happening to it and update of nr_sects
111 * can be non-atomic on 32bit machines with 64bit sector_t.
112 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 sector_t nr_sects;
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200114 seqcount_t nr_sects_seq;
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400115 sector_t alignment_offset;
Jens Axboea1706ac2011-05-30 07:42:51 +0200116 unsigned int discard_alignment;
Tejun Heoed9e1982008-08-25 19:56:05 +0900117 struct device __dev;
Jun'ichi Nomura6a4d44c2006-03-27 01:17:55 -0800118 struct kobject *holder_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 int policy, partno;
Will Drewry6d1d8052010-08-31 15:47:05 -0500120 struct partition_meta_info *info;
Akinobu Mitac17bb492006-12-08 02:39:46 -0800121#ifdef CONFIG_FAIL_MAKE_REQUEST
122 int make_it_fail;
123#endif
Jerome Marchandea5c48a2008-02-08 11:04:09 +0100124 unsigned long stamp;
Shaohua Li1e9bb882011-03-22 08:35:35 +0100125 atomic_t in_flight[2];
Jerome Marchandea5c48a2008-02-08 11:04:09 +0100126#ifdef CONFIG_SMP
Tejun Heo43cf38e2010-02-02 14:38:57 +0900127 struct disk_stats __percpu *dkstats;
Jerome Marchandea5c48a2008-02-08 11:04:09 +0100128#else
129 struct disk_stats dkstats;
130#endif
Ming Lei6c710132015-07-16 11:16:45 +0800131 struct percpu_ref ref;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200132 struct rcu_head rcu_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
135#define GENHD_FL_REMOVABLE 1
NeilBrown97fedbb2010-03-16 08:55:32 +0100136/* 2 is unused */
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700137#define GENHD_FL_MEDIA_CHANGE_NOTIFY 4
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#define GENHD_FL_CD 8
139#define GENHD_FL_UP 16
140#define GENHD_FL_SUPPRESS_PARTITION_INFO 32
Tejun Heo689d6fa2008-08-25 19:56:16 +0900141#define GENHD_FL_EXT_DEVT 64 /* allow extended devt */
Bartlomiej Zolnierkiewiczdb429e92009-06-07 13:52:52 +0200142#define GENHD_FL_NATIVE_CAPACITY 128
Tejun Heod4dc2102011-04-21 20:54:46 +0200143#define GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE 256
Tejun Heod27769e2011-08-23 20:01:04 +0200144#define GENHD_FL_NO_PART_SCAN 512
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300145#define GENHD_FL_HIDDEN 1024
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Tejun Heo77ea8872010-12-08 20:57:37 +0100147enum {
148 DISK_EVENT_MEDIA_CHANGE = 1 << 0, /* media changed */
149 DISK_EVENT_EJECT_REQUEST = 1 << 1, /* eject requested */
150};
151
Tejun Heo540eed52008-08-25 19:56:15 +0900152struct disk_part_tbl {
153 struct rcu_head rcu_head;
154 int len;
Arnd Bergmann4d2deb42010-02-24 20:01:56 +0100155 struct hd_struct __rcu *last_lookup;
156 struct hd_struct __rcu *part[];
Tejun Heo540eed52008-08-25 19:56:15 +0900157};
158
Tejun Heo77ea8872010-12-08 20:57:37 +0100159struct disk_events;
Vishal Verma99e66082016-01-09 08:36:51 -0800160struct badblocks;
Tejun Heo77ea8872010-12-08 20:57:37 +0100161
Martin K. Petersen25520d52015-10-21 13:19:49 -0400162#if defined(CONFIG_BLK_DEV_INTEGRITY)
163
164struct blk_integrity {
Eric Biggers869ab902017-03-24 18:03:48 -0700165 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;
Martin K. Petersen25520d52015-10-21 13:19:49 -0400170};
171
172#endif /* CONFIG_BLK_DEV_INTEGRITY */
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174struct gendisk {
Tejun Heo689d6fa2008-08-25 19:56:16 +0900175 /* major, first_minor and minors are input parameters only,
176 * don't use directly. Use disk_devt() and disk_max_parts().
Tejun Heof331c022008-09-03 09:01:48 +0200177 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 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. */
Tejun Heof331c022008-09-03 09:01:48 +0200182
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900183 char disk_name[DISK_NAME_LEN]; /* name of major driver */
Al Viro2c9ede52011-07-23 20:24:48 -0400184 char *(*devnode)(struct gendisk *gd, umode_t *mode);
Tejun Heo77ea8872010-12-08 20:57:37 +0100185
186 unsigned int events; /* supported events */
187 unsigned int async_events; /* async events, subset of all */
188
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200189 /* Array of pointers to partitions indexed by partno.
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200190 * Protected with matching bdev lock but stat and other
191 * non-critical accesses use RCU. Always access through
192 * helpers.
193 */
Arnd Bergmann4d2deb42010-02-24 20:01:56 +0100194 struct disk_part_tbl __rcu *part_tbl;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200195 struct hd_struct part0;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200196
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700197 const struct block_device_operations *fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct request_queue *queue;
199 void *private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 int flags;
Jan Kara56c09082018-02-26 13:01:41 +0100202 struct rw_semaphore lookup_sem;
Jun'ichi Nomura6a4d44c2006-03-27 01:17:55 -0800203 struct kobject *slave_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 struct timer_rand_state *random;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 atomic_t sync_io; /* RAID */
Tejun Heo77ea8872010-12-08 20:57:37 +0100207 struct disk_events *ev;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200208#ifdef CONFIG_BLK_DEV_INTEGRITY
Martin K. Petersenaff34e12015-10-21 13:19:27 -0400209 struct kobject integrity_kobj;
Martin K. Petersen25520d52015-10-21 13:19:49 -0400210#endif /* CONFIG_BLK_DEV_INTEGRITY */
Tejun Heo540eed52008-08-25 19:56:15 +0900211 int node_id;
Vishal Verma99e66082016-01-09 08:36:51 -0800212 struct badblocks *bb;
Byungchul Parke319e1f2017-10-25 17:56:05 +0900213 struct lockdep_map lockdep_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214};
215
Tejun Heo310a2c12008-08-25 19:47:17 +0900216static inline struct gendisk *part_to_disk(struct hd_struct *part)
217{
Tejun Heo548b10e2008-08-29 09:01:47 +0200218 if (likely(part)) {
219 if (part->partno)
220 return dev_to_disk(part_to_dev(part)->parent);
221 else
222 return dev_to_disk(part_to_dev(part));
223 }
Tejun Heo310a2c12008-08-25 19:47:17 +0900224 return NULL;
225}
226
Tejun Heof331c022008-09-03 09:01:48 +0200227static inline int disk_max_parts(struct gendisk *disk)
228{
Tejun Heo689d6fa2008-08-25 19:56:16 +0900229 if (disk->flags & GENHD_FL_EXT_DEVT)
230 return DISK_MAX_PARTS;
231 return disk->minors;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200232}
233
Tejun Heod27769e2011-08-23 20:01:04 +0200234static inline bool disk_part_scan_enabled(struct gendisk *disk)
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200235{
Tejun Heod27769e2011-08-23 20:01:04 +0200236 return disk_max_parts(disk) > 1 &&
237 !(disk->flags & GENHD_FL_NO_PART_SCAN);
Tejun Heof331c022008-09-03 09:01:48 +0200238}
239
240static inline dev_t disk_devt(struct gendisk *disk)
241{
Christoph Hellwig517bf3c2017-11-02 21:29:52 +0300242 return MKDEV(disk->major, disk->first_minor);
Tejun Heof331c022008-09-03 09:01:48 +0200243}
244
245static inline dev_t part_devt(struct hd_struct *part)
246{
Tejun Heoed9e1982008-08-25 19:56:05 +0900247 return part_to_dev(part)->devt;
Tejun Heof331c022008-09-03 09:01:48 +0200248}
249
Greg Edwards67f25192017-10-24 11:21:48 -0600250extern struct hd_struct *__disk_get_part(struct gendisk *disk, int partno);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200251extern struct hd_struct *disk_get_part(struct gendisk *disk, int partno);
252
253static inline void disk_put_part(struct hd_struct *part)
254{
255 if (likely(part))
Tejun Heoed9e1982008-08-25 19:56:05 +0900256 put_device(part_to_dev(part));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200257}
258
259/*
260 * Smarter partition iterator without context limits.
261 */
262#define DISK_PITER_REVERSE (1 << 0) /* iterate in the reverse direction */
263#define DISK_PITER_INCL_EMPTY (1 << 1) /* include 0-sized parts */
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200264#define DISK_PITER_INCL_PART0 (1 << 2) /* include partition 0 */
Tejun Heo71982a42009-04-17 08:34:48 +0200265#define DISK_PITER_INCL_EMPTY_PART0 (1 << 3) /* include empty partition 0 */
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200266
267struct disk_part_iter {
268 struct gendisk *disk;
269 struct hd_struct *part;
270 int idx;
271 unsigned int flags;
272};
273
274extern void disk_part_iter_init(struct disk_part_iter *piter,
275 struct gendisk *disk, unsigned int flags);
276extern struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter);
277extern void disk_part_iter_exit(struct disk_part_iter *piter);
278
279extern struct hd_struct *disk_map_sector_rcu(struct gendisk *disk,
280 sector_t sector);
281
Tejun Heoc9959052008-08-25 19:47:21 +0900282/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * Macros to operate on percpu disk statistics:
284 *
Tejun Heoc9959052008-08-25 19:47:21 +0900285 * {disk|part|all}_stat_{add|sub|inc|dec}() modify the stat counters
286 * and should be called between disk_stat_lock() and
287 * disk_stat_unlock().
288 *
289 * part_stat_read() can be called at any time.
290 *
291 * part_stat_{add|set_all}() and {init|free}_part_stats are for
292 * internal use only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 */
294#ifdef CONFIG_SMP
Tejun Heo074a7ac2008-08-25 19:56:14 +0900295#define part_stat_lock() ({ rcu_read_lock(); get_cpu(); })
296#define part_stat_unlock() do { put_cpu(); rcu_read_unlock(); } while (0)
Tejun Heoc9959052008-08-25 19:47:21 +0900297
Tejun Heo074a7ac2008-08-25 19:56:14 +0900298#define __part_stat_add(cpu, part, field, addnd) \
299 (per_cpu_ptr((part)->dkstats, (cpu))->field += (addnd))
Jerome Marchandea5c48a2008-02-08 11:04:09 +0100300
301#define part_stat_read(part, field) \
302({ \
Tejun Heo074a7ac2008-08-25 19:56:14 +0900303 typeof((part)->dkstats->field) res = 0; \
Stephen Hemminger7af92f82010-01-06 15:45:55 -0800304 unsigned int _cpu; \
305 for_each_possible_cpu(_cpu) \
306 res += per_cpu_ptr((part)->dkstats, _cpu)->field; \
Jerome Marchandea5c48a2008-02-08 11:04:09 +0100307 res; \
308})
309
Jens Axboe28f13702008-05-07 10:15:46 +0200310static inline void part_stat_set_all(struct hd_struct *part, int value)
311{
Jerome Marchandea5c48a2008-02-08 11:04:09 +0100312 int i;
Jens Axboe28f13702008-05-07 10:15:46 +0200313
Jerome Marchandea5c48a2008-02-08 11:04:09 +0100314 for_each_possible_cpu(i)
315 memset(per_cpu_ptr(part->dkstats, i), value,
Jens Axboe28f13702008-05-07 10:15:46 +0200316 sizeof(struct disk_stats));
Jerome Marchandea5c48a2008-02-08 11:04:09 +0100317}
Tejun Heoc9959052008-08-25 19:47:21 +0900318
Jerome Marchandea5c48a2008-02-08 11:04:09 +0100319static inline int init_part_stats(struct hd_struct *part)
320{
321 part->dkstats = alloc_percpu(struct disk_stats);
322 if (!part->dkstats)
323 return 0;
324 return 1;
325}
326
327static inline void free_part_stats(struct hd_struct *part)
328{
329 free_percpu(part->dkstats);
330}
331
Tejun Heo074a7ac2008-08-25 19:56:14 +0900332#else /* !CONFIG_SMP */
333#define part_stat_lock() ({ rcu_read_lock(); 0; })
334#define part_stat_unlock() rcu_read_unlock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Tejun Heo074a7ac2008-08-25 19:56:14 +0900336#define __part_stat_add(cpu, part, field, addnd) \
337 ((part)->dkstats.field += addnd)
338
339#define part_stat_read(part, field) ((part)->dkstats.field)
340
341static inline void part_stat_set_all(struct hd_struct *part, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Tejun Heo074a7ac2008-08-25 19:56:14 +0900343 memset(&part->dkstats, value, sizeof(struct disk_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
Jerome Marchandea5c48a2008-02-08 11:04:09 +0100345
346static inline int init_part_stats(struct hd_struct *part)
347{
348 return 1;
349}
350
351static inline void free_part_stats(struct hd_struct *part)
352{
353}
Tejun Heo074a7ac2008-08-25 19:56:14 +0900354
355#endif /* CONFIG_SMP */
356
Michael Callahan59767fb2018-07-18 04:47:37 -0700357#define part_stat_read_accum(part, field) \
Michael Callahandbae2c52018-07-18 04:47:38 -0700358 (part_stat_read(part, field[STAT_READ]) + \
Michael Callahanbdca3c82018-07-18 04:47:40 -0700359 part_stat_read(part, field[STAT_WRITE]) + \
360 part_stat_read(part, field[STAT_DISCARD]))
Michael Callahan59767fb2018-07-18 04:47:37 -0700361
Tejun Heo074a7ac2008-08-25 19:56:14 +0900362#define part_stat_add(cpu, part, field, addnd) do { \
363 __part_stat_add((cpu), (part), field, addnd); \
364 if ((part)->partno) \
365 __part_stat_add((cpu), &part_to_disk((part))->part0, \
366 field, addnd); \
367} while (0)
368
369#define part_stat_dec(cpu, gendiskp, field) \
370 part_stat_add(cpu, gendiskp, field, -1)
371#define part_stat_inc(cpu, gendiskp, field) \
372 part_stat_add(cpu, gendiskp, field, 1)
373#define part_stat_sub(cpu, gendiskp, field, subnd) \
374 part_stat_add(cpu, gendiskp, field, -subnd)
375
Jens Axboef299b7c2017-08-08 17:51:45 -0600376void part_in_flight(struct request_queue *q, struct hd_struct *part,
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700377 unsigned int inflight[2]);
378void part_in_flight_rw(struct request_queue *q, struct hd_struct *part,
379 unsigned int inflight[2]);
Jens Axboef299b7c2017-08-08 17:51:45 -0600380void part_dec_in_flight(struct request_queue *q, struct hd_struct *part,
381 int rw);
382void part_inc_in_flight(struct request_queue *q, struct hd_struct *part,
383 int rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Will Drewry6d1d8052010-08-31 15:47:05 -0500385static inline struct partition_meta_info *alloc_part_info(struct gendisk *disk)
386{
387 if (disk)
388 return kzalloc_node(sizeof(struct partition_meta_info),
389 GFP_KERNEL, disk->node_id);
390 return kzalloc(sizeof(struct partition_meta_info), GFP_KERNEL);
391}
392
393static inline void free_part_info(struct hd_struct *part)
394{
395 kfree(part->info);
396}
397
Petros Koutoupisd3992282009-03-11 10:49:35 +0100398/* block/blk-core.c */
Jens Axboed62e26b2017-06-30 21:55:08 -0600399extern void part_round_stats(struct request_queue *q, int cpu, struct hd_struct *part);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Petros Koutoupis32ca1632009-03-10 08:25:54 +0100401/* block/genhd.c */
Hannes Reineckefef912b2018-09-28 08:17:19 +0200402extern void device_add_disk(struct device *parent, struct gendisk *disk,
403 const struct attribute_group **groups);
Dan Williamse63a46b2016-06-15 18:17:27 -0700404static inline void add_disk(struct gendisk *disk)
405{
Hannes Reineckefef912b2018-09-28 08:17:19 +0200406 device_add_disk(NULL, disk, NULL);
Dan Williamse63a46b2016-06-15 18:17:27 -0700407}
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500408extern void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk);
409static inline void add_disk_no_queue_reg(struct gendisk *disk)
410{
411 device_add_disk_no_queue_reg(NULL, disk);
412}
Dan Williamse63a46b2016-06-15 18:17:27 -0700413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414extern void del_gendisk(struct gendisk *gp);
Tejun Heocf771cb2008-09-03 09:01:09 +0200415extern struct gendisk *get_gendisk(dev_t dev, int *partno);
Tejun Heof331c022008-09-03 09:01:48 +0200416extern struct block_device *bdget_disk(struct gendisk *disk, int partno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418extern void set_device_ro(struct block_device *bdev, int flag);
419extern void set_disk_ro(struct gendisk *disk, int flag);
420
Tejun Heob7db9952008-08-25 19:56:10 +0900421static inline int get_disk_ro(struct gendisk *disk)
422{
423 return disk->part0.policy;
424}
425
Tejun Heo77ea8872010-12-08 20:57:37 +0100426extern void disk_block_events(struct gendisk *disk);
427extern void disk_unblock_events(struct gendisk *disk);
Tejun Heo85ef06d2011-07-01 16:17:47 +0200428extern void disk_flush_events(struct gendisk *disk, unsigned int mask);
Tejun Heo77ea8872010-12-08 20:57:37 +0100429extern unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask);
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/* drivers/char/random.c */
Emese Revfy0766f782016-06-20 20:42:34 +0200432extern void add_disk_randomness(struct gendisk *disk) __latent_entropy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433extern void rand_initialize_disk(struct gendisk *disk);
434
435static inline sector_t get_start_sect(struct block_device *bdev)
436{
Tejun Heo0762b8b2008-08-25 19:56:12 +0900437 return bdev->bd_part->start_sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439static inline sector_t get_capacity(struct gendisk *disk)
440{
Tejun Heo80795ae2008-08-25 19:56:07 +0900441 return disk->part0.nr_sects;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443static inline void set_capacity(struct gendisk *disk, sector_t size)
444{
Tejun Heo80795ae2008-08-25 19:56:07 +0900445 disk->part0.nr_sects = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448#ifdef CONFIG_SOLARIS_X86_PARTITION
449
Mark Fortescueb84d8792007-07-25 18:30:08 -0700450#define SOLARIS_X86_NUMSLICE 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451#define SOLARIS_X86_VTOC_SANE (0x600DDEEEUL)
452
453struct solaris_x86_slice {
454 __le16 s_tag; /* ID tag of partition */
455 __le16 s_flag; /* permission flags */
456 __le32 s_start; /* start sector no of partition */
457 __le32 s_size; /* # of blocks in partition */
458};
459
460struct solaris_x86_vtoc {
461 unsigned int v_bootinfo[3]; /* info needed by mboot (unsupported) */
462 __le32 v_sanity; /* to verify vtoc sanity */
463 __le32 v_version; /* layout version */
464 char v_volume[8]; /* volume name */
465 __le16 v_sectorsz; /* sector size in bytes */
466 __le16 v_nparts; /* number of partitions */
467 unsigned int v_reserved[10]; /* free space */
468 struct solaris_x86_slice
469 v_slice[SOLARIS_X86_NUMSLICE]; /* slice headers */
470 unsigned int timestamp[SOLARIS_X86_NUMSLICE]; /* timestamp (unsupported) */
471 char v_asciilabel[128]; /* for compatibility */
472};
473
474#endif /* CONFIG_SOLARIS_X86_PARTITION */
475
476#ifdef CONFIG_BSD_DISKLABEL
477/*
478 * BSD disklabel support by Yossi Gottlieb <yogo@math.tau.ac.il>
479 * updated by Marc Espie <Marc.Espie@openbsd.org>
480 */
481
482/* check against BSD src/sys/sys/disklabel.h for consistency */
483
484#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
485#define BSD_MAXPARTITIONS 16
486#define OPENBSD_MAXPARTITIONS 16
487#define BSD_FS_UNUSED 0 /* disklabel unused partition entry ID */
488struct bsd_disklabel {
489 __le32 d_magic; /* the magic number */
490 __s16 d_type; /* drive type */
491 __s16 d_subtype; /* controller/d_type specific */
492 char d_typename[16]; /* type name, e.g. "eagle" */
493 char d_packname[16]; /* pack identifier */
494 __u32 d_secsize; /* # of bytes per sector */
495 __u32 d_nsectors; /* # of data sectors per track */
496 __u32 d_ntracks; /* # of tracks per cylinder */
497 __u32 d_ncylinders; /* # of data cylinders per unit */
498 __u32 d_secpercyl; /* # of data sectors per cylinder */
499 __u32 d_secperunit; /* # of data sectors per unit */
500 __u16 d_sparespertrack; /* # of spare sectors per track */
501 __u16 d_sparespercyl; /* # of spare sectors per cylinder */
502 __u32 d_acylinders; /* # of alt. cylinders per unit */
503 __u16 d_rpm; /* rotational speed */
504 __u16 d_interleave; /* hardware sector interleave */
505 __u16 d_trackskew; /* sector 0 skew, per track */
506 __u16 d_cylskew; /* sector 0 skew, per cylinder */
507 __u32 d_headswitch; /* head switch time, usec */
508 __u32 d_trkseek; /* track-to-track seek, usec */
509 __u32 d_flags; /* generic flags */
510#define NDDATA 5
511 __u32 d_drivedata[NDDATA]; /* drive-type specific information */
512#define NSPARE 5
513 __u32 d_spare[NSPARE]; /* reserved for future use */
514 __le32 d_magic2; /* the magic number (again) */
515 __le16 d_checksum; /* xor of data incl. partitions */
516
517 /* filesystem and partition information: */
518 __le16 d_npartitions; /* number of partitions in following */
519 __le32 d_bbsize; /* size of boot area at sn0, bytes */
520 __le32 d_sbsize; /* max size of fs superblock, bytes */
521 struct bsd_partition { /* the partition table */
522 __le32 p_size; /* number of sectors in partition */
523 __le32 p_offset; /* starting sector */
524 __le32 p_fsize; /* filesystem basic fragment size */
525 __u8 p_fstype; /* filesystem type, see below */
526 __u8 p_frag; /* filesystem fragments per block */
527 __le16 p_cpg; /* filesystem cylinders per group */
528 } d_partitions[BSD_MAXPARTITIONS]; /* actually may be more */
529};
530
531#endif /* CONFIG_BSD_DISKLABEL */
532
533#ifdef CONFIG_UNIXWARE_DISKLABEL
534/*
535 * Unixware slices support by Andrzej Krzysztofowicz <ankry@mif.pg.gda.pl>
536 * and Krzysztof G. Baranowski <kgb@knm.org.pl>
537 */
538
539#define UNIXWARE_DISKMAGIC (0xCA5E600DUL) /* The disk magic number */
540#define UNIXWARE_DISKMAGIC2 (0x600DDEEEUL) /* The slice table magic nr */
541#define UNIXWARE_NUMSLICE 16
542#define UNIXWARE_FS_UNUSED 0 /* Unused slice entry ID */
543
544struct unixware_slice {
545 __le16 s_label; /* label */
546 __le16 s_flags; /* permission flags */
547 __le32 start_sect; /* starting sector */
548 __le32 nr_sects; /* number of sectors in slice */
549};
550
551struct unixware_disklabel {
552 __le32 d_type; /* drive type */
553 __le32 d_magic; /* the magic number */
554 __le32 d_version; /* version number */
555 char d_serial[12]; /* serial number of the device */
556 __le32 d_ncylinders; /* # of data cylinders per device */
557 __le32 d_ntracks; /* # of tracks per cylinder */
558 __le32 d_nsectors; /* # of data sectors per track */
559 __le32 d_secsize; /* # of bytes per sector */
560 __le32 d_part_start; /* # of first sector of this partition */
561 __le32 d_unknown1[12]; /* ? */
562 __le32 d_alt_tbl; /* byte offset of alternate table */
563 __le32 d_alt_len; /* byte length of alternate table */
564 __le32 d_phys_cyl; /* # of physical cylinders per device */
565 __le32 d_phys_trk; /* # of physical tracks per cylinder */
566 __le32 d_phys_sec; /* # of physical sectors per track */
567 __le32 d_phys_bytes; /* # of physical bytes per sector */
568 __le32 d_unknown2; /* ? */
569 __le32 d_unknown3; /* ? */
570 __le32 d_pad[8]; /* pad */
571
572 struct unixware_vtoc {
573 __le32 v_magic; /* the magic number */
574 __le32 v_version; /* version number */
575 char v_name[8]; /* volume name */
576 __le16 v_nslices; /* # of slices */
577 __le16 v_unknown1; /* ? */
578 __le32 v_reserved[10]; /* reserved */
579 struct unixware_slice
580 v_slice[UNIXWARE_NUMSLICE]; /* slice headers */
581 } vtoc;
582
583}; /* 408 */
584
585#endif /* CONFIG_UNIXWARE_DISKLABEL */
586
587#ifdef CONFIG_MINIX_SUBPARTITION
588# define MINIX_NR_SUBPARTITIONS 4
589#endif /* CONFIG_MINIX_SUBPARTITION */
590
Fabio Massimo Di Nittod18d7682007-02-10 23:50:00 -0800591#define ADDPART_FLAG_NONE 0
592#define ADDPART_FLAG_RAID 1
593#define ADDPART_FLAG_WHOLEDISK 2
594
Tejun Heobcce3de2008-08-25 19:47:22 +0900595extern int blk_alloc_devt(struct hd_struct *part, dev_t *devt);
596extern void blk_free_devt(dev_t devt);
Tejun Heocf771cb2008-09-03 09:01:09 +0200597extern dev_t blk_lookup_devt(const char *name, int partno);
598extern char *disk_name (struct gendisk *hd, int partno, char *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Tejun Heo540eed52008-08-25 19:56:15 +0900600extern int disk_expand_part_tbl(struct gendisk *disk, int target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601extern int rescan_partitions(struct gendisk *disk, struct block_device *bdev);
Jun'ichi Nomurafe316bf2012-03-02 10:38:33 +0100602extern int invalidate_partitions(struct gendisk *disk, struct block_device *bdev);
Tejun Heoba329292008-11-10 15:29:58 +0900603extern struct hd_struct * __must_check add_partition(struct gendisk *disk,
604 int partno, sector_t start,
Will Drewry6d1d8052010-08-31 15:47:05 -0500605 sector_t len, int flags,
606 struct partition_meta_info
607 *info);
Ming Lei6c710132015-07-16 11:16:45 +0800608extern void __delete_partition(struct percpu_ref *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609extern void delete_partition(struct gendisk *, int);
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700610extern void printk_all_partitions(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Byungchul Parke319e1f2017-10-25 17:56:05 +0900612extern struct gendisk *__alloc_disk_node(int minors, int node_id);
Jan Kara3079c222018-02-26 13:01:38 +0100613extern struct kobject *get_disk_and_module(struct gendisk *disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614extern void put_disk(struct gendisk *disk);
Jan Kara9df6c292018-02-26 13:01:39 +0100615extern void put_disk_and_module(struct gendisk *disk);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200616extern void blk_register_region(dev_t devt, unsigned long range,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 struct module *module,
618 struct kobject *(*probe)(dev_t, int *, void *),
619 int (*lock)(dev_t, void *),
620 void *data);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200621extern void blk_unregister_region(dev_t devt, unsigned long range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Tejun Heoe5610522008-08-25 19:56:09 +0900623extern ssize_t part_size_show(struct device *dev,
624 struct device_attribute *attr, char *buf);
Tejun Heo074a7ac2008-08-25 19:56:14 +0900625extern ssize_t part_stat_show(struct device *dev,
626 struct device_attribute *attr, char *buf);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200627extern ssize_t part_inflight_show(struct device *dev,
628 struct device_attribute *attr, char *buf);
Tejun Heoeddb2e22008-08-25 19:56:13 +0900629#ifdef CONFIG_FAIL_MAKE_REQUEST
630extern ssize_t part_fail_show(struct device *dev,
631 struct device_attribute *attr, char *buf);
632extern ssize_t part_fail_store(struct device *dev,
633 struct device_attribute *attr,
634 const char *buf, size_t count);
635#endif /* CONFIG_FAIL_MAKE_REQUEST */
Tejun Heoe5610522008-08-25 19:56:09 +0900636
Byungchul Parke319e1f2017-10-25 17:56:05 +0900637#define alloc_disk_node(minors, node_id) \
638({ \
639 static struct lock_class_key __key; \
640 const char *__name; \
641 struct gendisk *__disk; \
642 \
643 __name = "(gendisk_completion)"#minors"("#node_id")"; \
644 \
645 __disk = __alloc_disk_node(minors, node_id); \
646 \
647 if (__disk) \
648 lockdep_init_map(&__disk->lockdep_map, __name, &__key, 0); \
649 \
650 __disk; \
651})
652
653#define alloc_disk(minors) alloc_disk_node(minors, NUMA_NO_NODE)
654
Ming Lei6c710132015-07-16 11:16:45 +0800655static inline int hd_ref_init(struct hd_struct *part)
Jens Axboe6c23a962011-01-07 08:43:37 +0100656{
Ming Lei6c710132015-07-16 11:16:45 +0800657 if (percpu_ref_init(&part->ref, __delete_partition, 0,
658 GFP_KERNEL))
659 return -ENOMEM;
660 return 0;
Jens Axboe6c23a962011-01-07 08:43:37 +0100661}
662
663static inline void hd_struct_get(struct hd_struct *part)
664{
Ming Lei6c710132015-07-16 11:16:45 +0800665 percpu_ref_get(&part->ref);
Jens Axboe6c23a962011-01-07 08:43:37 +0100666}
667
668static inline int hd_struct_try_get(struct hd_struct *part)
669{
Ming Lei6c710132015-07-16 11:16:45 +0800670 return percpu_ref_tryget_live(&part->ref);
Jens Axboe6c23a962011-01-07 08:43:37 +0100671}
672
673static inline void hd_struct_put(struct hd_struct *part)
674{
Ming Lei6c710132015-07-16 11:16:45 +0800675 percpu_ref_put(&part->ref);
676}
677
678static inline void hd_struct_kill(struct hd_struct *part)
679{
680 percpu_ref_kill(&part->ref);
Jens Axboe6c23a962011-01-07 08:43:37 +0100681}
682
Ming Leib54e5ed2015-07-16 11:16:44 +0800683static inline void hd_free_part(struct hd_struct *part)
684{
685 free_part_stats(part);
686 free_part_info(part);
Ming Lei6c710132015-07-16 11:16:45 +0800687 percpu_ref_exit(&part->ref);
Ming Leib54e5ed2015-07-16 11:16:44 +0800688}
689
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200690/*
691 * Any access of part->nr_sects which is not protected by partition
692 * bd_mutex or gendisk bdev bd_mutex, should be done using this
693 * accessor function.
694 *
695 * Code written along the lines of i_size_read() and i_size_write().
696 * CONFIG_PREEMPT case optimizes the case of UP kernel with preemption
697 * on.
698 */
699static inline sector_t part_nr_sects_read(struct hd_struct *part)
700{
701#if BITS_PER_LONG==32 && defined(CONFIG_LBDAF) && defined(CONFIG_SMP)
702 sector_t nr_sects;
703 unsigned seq;
704 do {
705 seq = read_seqcount_begin(&part->nr_sects_seq);
706 nr_sects = part->nr_sects;
707 } while (read_seqcount_retry(&part->nr_sects_seq, seq));
708 return nr_sects;
709#elif BITS_PER_LONG==32 && defined(CONFIG_LBDAF) && defined(CONFIG_PREEMPT)
710 sector_t nr_sects;
711
712 preempt_disable();
713 nr_sects = part->nr_sects;
714 preempt_enable();
715 return nr_sects;
716#else
717 return part->nr_sects;
718#endif
719}
720
721/*
722 * Should be called with mutex lock held (typically bd_mutex) of partition
723 * to provide mutual exlusion among writers otherwise seqcount might be
724 * left in wrong state leaving the readers spinning infinitely.
725 */
726static inline void part_nr_sects_write(struct hd_struct *part, sector_t size)
727{
728#if BITS_PER_LONG==32 && defined(CONFIG_LBDAF) && defined(CONFIG_SMP)
729 write_seqcount_begin(&part->nr_sects_seq);
730 part->nr_sects = size;
731 write_seqcount_end(&part->nr_sects_seq);
732#elif BITS_PER_LONG==32 && defined(CONFIG_LBDAF) && defined(CONFIG_PREEMPT)
733 preempt_disable();
734 part->nr_sects = size;
735 preempt_enable();
736#else
737 part->nr_sects = size;
738#endif
739}
740
Martin K. Petersen25520d52015-10-21 13:19:49 -0400741#if defined(CONFIG_BLK_DEV_INTEGRITY)
742extern void blk_integrity_add(struct gendisk *);
743extern void blk_integrity_del(struct gendisk *);
Martin K. Petersen25520d52015-10-21 13:19:49 -0400744#else /* CONFIG_BLK_DEV_INTEGRITY */
745static inline void blk_integrity_add(struct gendisk *disk) { }
746static inline void blk_integrity_del(struct gendisk *disk) { }
Martin K. Petersen25520d52015-10-21 13:19:49 -0400747#endif /* CONFIG_BLK_DEV_INTEGRITY */
748
Jens Axboe87c1efb2007-05-11 13:29:54 +0200749#else /* CONFIG_BLOCK */
750
751static inline void printk_all_partitions(void) { }
752
Tejun Heocf771cb2008-09-03 09:01:09 +0200753static inline dev_t blk_lookup_devt(const char *name, int partno)
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200754{
755 dev_t devt = MKDEV(0, 0);
756 return devt;
757}
Jens Axboe87c1efb2007-05-11 13:29:54 +0200758#endif /* CONFIG_BLOCK */
David Howells93614012006-09-30 20:45:40 +0200759
David Woodhousea8ae50b2008-03-12 17:52:56 +0100760#endif /* _LINUX_GENHD_H */