block: remove alloc_part_info and free_part_info
[platform/kernel/linux-starfive.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 kobject *block_depr;
31 extern struct class block_class;
32
33 enum {
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
40         SUN_WHOLE_DISK = DOS_EXTENDED_PARTITION,
41
42         LINUX_SWAP_PARTITION = 0x82,
43         LINUX_DATA_PARTITION = 0x83,
44         LINUX_LVM_PARTITION = 0x8e,
45         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
63 #define DISK_MAX_PARTS                  256
64 #define DISK_NAME_LEN                   32
65
66 #include <linux/major.h>
67 #include <linux/device.h>
68 #include <linux/smp.h>
69 #include <linux/string.h>
70 #include <linux/fs.h>
71 #include <linux/workqueue.h>
72
73 struct 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
86 struct disk_stats {
87         u64 nsecs[NR_STAT_GROUPS];
88         unsigned long sectors[NR_STAT_GROUPS];
89         unsigned long ios[NR_STAT_GROUPS];
90         unsigned long merges[NR_STAT_GROUPS];
91         unsigned long io_ticks;
92         unsigned long time_in_queue;
93         local_t in_flight[2];
94 };
95
96 #define PARTITION_META_INFO_VOLNAMELTH  64
97 /*
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  */
101 #define PARTITION_META_INFO_UUIDLTH     (UUID_STRING_LEN + 1)
102
103 struct partition_meta_info {
104         char uuid[PARTITION_META_INFO_UUIDLTH];
105         u8 volname[PARTITION_META_INFO_VOLNAMELTH];
106 };
107
108 struct hd_struct {
109         sector_t start_sect;
110         /*
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          */
115         sector_t nr_sects;
116         seqcount_t nr_sects_seq;
117         sector_t alignment_offset;
118         unsigned int discard_alignment;
119         struct device __dev;
120         struct kobject *holder_dir;
121         int policy, partno;
122         struct partition_meta_info *info;
123 #ifdef CONFIG_FAIL_MAKE_REQUEST
124         int make_it_fail;
125 #endif
126         unsigned long stamp;
127 #ifdef  CONFIG_SMP
128         struct disk_stats __percpu *dkstats;
129 #else
130         struct disk_stats dkstats;
131 #endif
132         struct percpu_ref ref;
133         struct rcu_work rcu_work;
134 };
135
136 /**
137  * DOC: genhd capability flags
138  *
139  * ``GENHD_FL_REMOVABLE`` (0x0001): indicates that the block device
140  * gives access to removable media.
141  * When set, the device remains present even when media is not
142  * inserted.
143  * Must not be set for devices which are removed entirely when the
144  * media is removed.
145  *
146  * ``GENHD_FL_CD`` (0x0008): the block device is a CD-ROM-style
147  * device.
148  * Affects responses to the ``CDROM_GET_CAPABILITY`` ioctl.
149  *
150  * ``GENHD_FL_UP`` (0x0010): indicates that the block device is "up",
151  * with a similar meaning to network interfaces.
152  *
153  * ``GENHD_FL_SUPPRESS_PARTITION_INFO`` (0x0020): don't include
154  * partition information in ``/proc/partitions`` or in the output of
155  * printk_all_partitions().
156  * Used for the null block device and some MMC devices.
157  *
158  * ``GENHD_FL_EXT_DEVT`` (0x0040): the driver supports extended
159  * dynamic ``dev_t``, i.e. it wants extended device numbers
160  * (``BLOCK_EXT_MAJOR``).
161  * This affects the maximum number of partitions.
162  *
163  * ``GENHD_FL_NATIVE_CAPACITY`` (0x0080): based on information in the
164  * partition table, the device's capacity has been extended to its
165  * native capacity; i.e. the device has hidden capacity used by one
166  * of the partitions (this is a flag used so that native capacity is
167  * only ever unlocked once).
168  *
169  * ``GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE`` (0x0100): event polling is
170  * blocked whenever a writer holds an exclusive lock.
171  *
172  * ``GENHD_FL_NO_PART_SCAN`` (0x0200): partition scanning is disabled.
173  * Used for loop devices in their default settings and some MMC
174  * devices.
175  *
176  * ``GENHD_FL_HIDDEN`` (0x0400): the block device is hidden; it
177  * doesn't produce events, doesn't appear in sysfs, and doesn't have
178  * an associated ``bdev``.
179  * Implies ``GENHD_FL_SUPPRESS_PARTITION_INFO`` and
180  * ``GENHD_FL_NO_PART_SCAN``.
181  * Used for multipath devices.
182  */
183 #define GENHD_FL_REMOVABLE                      0x0001
184 /* 2 is unused (used to be GENHD_FL_DRIVERFS) */
185 /* 4 is unused (used to be GENHD_FL_MEDIA_CHANGE_NOTIFY) */
186 #define GENHD_FL_CD                             0x0008
187 #define GENHD_FL_UP                             0x0010
188 #define GENHD_FL_SUPPRESS_PARTITION_INFO        0x0020
189 #define GENHD_FL_EXT_DEVT                       0x0040
190 #define GENHD_FL_NATIVE_CAPACITY                0x0080
191 #define GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE     0x0100
192 #define GENHD_FL_NO_PART_SCAN                   0x0200
193 #define GENHD_FL_HIDDEN                         0x0400
194
195 enum {
196         DISK_EVENT_MEDIA_CHANGE                 = 1 << 0, /* media changed */
197         DISK_EVENT_EJECT_REQUEST                = 1 << 1, /* eject requested */
198 };
199
200 enum {
201         /* Poll even if events_poll_msecs is unset */
202         DISK_EVENT_FLAG_POLL                    = 1 << 0,
203         /* Forward events to udev */
204         DISK_EVENT_FLAG_UEVENT                  = 1 << 1,
205 };
206
207 struct disk_part_tbl {
208         struct rcu_head rcu_head;
209         int len;
210         struct hd_struct __rcu *last_lookup;
211         struct hd_struct __rcu *part[];
212 };
213
214 struct disk_events;
215 struct badblocks;
216
217 #if defined(CONFIG_BLK_DEV_INTEGRITY)
218
219 struct blk_integrity {
220         const struct blk_integrity_profile      *profile;
221         unsigned char                           flags;
222         unsigned char                           tuple_size;
223         unsigned char                           interval_exp;
224         unsigned char                           tag_size;
225 };
226
227 #endif  /* CONFIG_BLK_DEV_INTEGRITY */
228
229 struct gendisk {
230         /* major, first_minor and minors are input parameters only,
231          * don't use directly.  Use disk_devt() and disk_max_parts().
232          */
233         int major;                      /* major number of driver */
234         int first_minor;
235         int minors;                     /* maximum number of minors, =1 for
236                                          * disks that can't be partitioned. */
237
238         char disk_name[DISK_NAME_LEN];  /* name of major driver */
239         char *(*devnode)(struct gendisk *gd, umode_t *mode);
240
241         unsigned short events;          /* supported events */
242         unsigned short event_flags;     /* flags related to event processing */
243
244         /* Array of pointers to partitions indexed by partno.
245          * Protected with matching bdev lock but stat and other
246          * non-critical accesses use RCU.  Always access through
247          * helpers.
248          */
249         struct disk_part_tbl __rcu *part_tbl;
250         struct hd_struct part0;
251
252         const struct block_device_operations *fops;
253         struct request_queue *queue;
254         void *private_data;
255
256         int flags;
257         struct rw_semaphore lookup_sem;
258         struct kobject *slave_dir;
259
260         struct timer_rand_state *random;
261         atomic_t sync_io;               /* RAID */
262         struct disk_events *ev;
263 #ifdef  CONFIG_BLK_DEV_INTEGRITY
264         struct kobject integrity_kobj;
265 #endif  /* CONFIG_BLK_DEV_INTEGRITY */
266         int node_id;
267         struct badblocks *bb;
268         struct lockdep_map lockdep_map;
269 };
270
271 static inline struct gendisk *part_to_disk(struct hd_struct *part)
272 {
273         if (likely(part)) {
274                 if (part->partno)
275                         return dev_to_disk(part_to_dev(part)->parent);
276                 else
277                         return dev_to_disk(part_to_dev(part));
278         }
279         return NULL;
280 }
281
282 static inline int disk_max_parts(struct gendisk *disk)
283 {
284         if (disk->flags & GENHD_FL_EXT_DEVT)
285                 return DISK_MAX_PARTS;
286         return disk->minors;
287 }
288
289 static inline bool disk_part_scan_enabled(struct gendisk *disk)
290 {
291         return disk_max_parts(disk) > 1 &&
292                 !(disk->flags & GENHD_FL_NO_PART_SCAN);
293 }
294
295 static inline bool disk_has_partitions(struct gendisk *disk)
296 {
297         bool ret = false;
298
299         rcu_read_lock();
300         if (rcu_dereference(disk->part_tbl)->len > 1)
301                 ret = true;
302         rcu_read_unlock();
303
304         return ret;
305 }
306
307 static inline dev_t disk_devt(struct gendisk *disk)
308 {
309         return MKDEV(disk->major, disk->first_minor);
310 }
311
312 static inline dev_t part_devt(struct hd_struct *part)
313 {
314         return part_to_dev(part)->devt;
315 }
316
317 extern struct hd_struct *__disk_get_part(struct gendisk *disk, int partno);
318 extern struct hd_struct *disk_get_part(struct gendisk *disk, int partno);
319
320 static inline void disk_put_part(struct hd_struct *part)
321 {
322         if (likely(part))
323                 put_device(part_to_dev(part));
324 }
325
326 /*
327  * Smarter partition iterator without context limits.
328  */
329 #define DISK_PITER_REVERSE      (1 << 0) /* iterate in the reverse direction */
330 #define DISK_PITER_INCL_EMPTY   (1 << 1) /* include 0-sized parts */
331 #define DISK_PITER_INCL_PART0   (1 << 2) /* include partition 0 */
332 #define DISK_PITER_INCL_EMPTY_PART0 (1 << 3) /* include empty partition 0 */
333
334 struct disk_part_iter {
335         struct gendisk          *disk;
336         struct hd_struct        *part;
337         int                     idx;
338         unsigned int            flags;
339 };
340
341 extern void disk_part_iter_init(struct disk_part_iter *piter,
342                                  struct gendisk *disk, unsigned int flags);
343 extern struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter);
344 extern void disk_part_iter_exit(struct disk_part_iter *piter);
345
346 extern struct hd_struct *disk_map_sector_rcu(struct gendisk *disk,
347                                              sector_t sector);
348
349 /*
350  * Macros to operate on percpu disk statistics:
351  *
352  * {disk|part|all}_stat_{add|sub|inc|dec}() modify the stat counters
353  * and should be called between disk_stat_lock() and
354  * disk_stat_unlock().
355  *
356  * part_stat_read() can be called at any time.
357  *
358  * part_stat_{add|set_all}() and {init|free}_part_stats are for
359  * internal use only.
360  */
361 #ifdef  CONFIG_SMP
362 #define part_stat_lock()        ({ rcu_read_lock(); get_cpu(); })
363 #define part_stat_unlock()      do { put_cpu(); rcu_read_unlock(); } while (0)
364
365 #define part_stat_get_cpu(part, field, cpu)                                     \
366         (per_cpu_ptr((part)->dkstats, (cpu))->field)
367
368 #define part_stat_get(part, field)                                      \
369         part_stat_get_cpu(part, field, smp_processor_id())
370
371 #define part_stat_read(part, field)                                     \
372 ({                                                                      \
373         typeof((part)->dkstats->field) res = 0;                         \
374         unsigned int _cpu;                                              \
375         for_each_possible_cpu(_cpu)                                     \
376                 res += per_cpu_ptr((part)->dkstats, _cpu)->field;       \
377         res;                                                            \
378 })
379
380 static inline void part_stat_set_all(struct hd_struct *part, int value)
381 {
382         int i;
383
384         for_each_possible_cpu(i)
385                 memset(per_cpu_ptr(part->dkstats, i), value,
386                                 sizeof(struct disk_stats));
387 }
388
389 static inline int init_part_stats(struct hd_struct *part)
390 {
391         part->dkstats = alloc_percpu(struct disk_stats);
392         if (!part->dkstats)
393                 return 0;
394         return 1;
395 }
396
397 static inline void free_part_stats(struct hd_struct *part)
398 {
399         free_percpu(part->dkstats);
400 }
401
402 #else /* !CONFIG_SMP */
403 #define part_stat_lock()        ({ rcu_read_lock(); 0; })
404 #define part_stat_unlock()      rcu_read_unlock()
405
406 #define part_stat_get(part, field)              ((part)->dkstats.field)
407 #define part_stat_get_cpu(part, field, cpu)     part_stat_get(part, field)
408 #define part_stat_read(part, field)             part_stat_get(part, field)
409
410 static inline void part_stat_set_all(struct hd_struct *part, int value)
411 {
412         memset(&part->dkstats, value, sizeof(struct disk_stats));
413 }
414
415 static inline int init_part_stats(struct hd_struct *part)
416 {
417         return 1;
418 }
419
420 static inline void free_part_stats(struct hd_struct *part)
421 {
422 }
423
424 #endif /* CONFIG_SMP */
425
426 #define part_stat_read_msecs(part, which)                               \
427         div_u64(part_stat_read(part, nsecs[which]), NSEC_PER_MSEC)
428
429 #define part_stat_read_accum(part, field)                               \
430         (part_stat_read(part, field[STAT_READ]) +                       \
431          part_stat_read(part, field[STAT_WRITE]) +                      \
432          part_stat_read(part, field[STAT_DISCARD]))
433
434 #define __part_stat_add(part, field, addnd)                             \
435         (part_stat_get(part, field) += (addnd))
436
437 #define part_stat_add(part, field, addnd)       do {                    \
438         __part_stat_add((part), field, addnd);                          \
439         if ((part)->partno)                                             \
440                 __part_stat_add(&part_to_disk((part))->part0,           \
441                                 field, addnd);                          \
442 } while (0)
443
444 #define part_stat_dec(gendiskp, field)                                  \
445         part_stat_add(gendiskp, field, -1)
446 #define part_stat_inc(gendiskp, field)                                  \
447         part_stat_add(gendiskp, field, 1)
448 #define part_stat_sub(gendiskp, field, subnd)                           \
449         part_stat_add(gendiskp, field, -subnd)
450
451 #define part_stat_local_dec(gendiskp, field)                            \
452         local_dec(&(part_stat_get(gendiskp, field)))
453 #define part_stat_local_inc(gendiskp, field)                            \
454         local_inc(&(part_stat_get(gendiskp, field)))
455 #define part_stat_local_read(gendiskp, field)                           \
456         local_read(&(part_stat_get(gendiskp, field)))
457 #define part_stat_local_read_cpu(gendiskp, field, cpu)                  \
458         local_read(&(part_stat_get_cpu(gendiskp, field, cpu)))
459
460 unsigned int part_in_flight(struct request_queue *q, struct hd_struct *part);
461 void part_in_flight_rw(struct request_queue *q, struct hd_struct *part,
462                        unsigned int inflight[2]);
463 void part_dec_in_flight(struct request_queue *q, struct hd_struct *part,
464                         int rw);
465 void part_inc_in_flight(struct request_queue *q, struct hd_struct *part,
466                         int rw);
467
468 void update_io_ticks(struct hd_struct *part, unsigned long now);
469
470 /* block/genhd.c */
471 extern void device_add_disk(struct device *parent, struct gendisk *disk,
472                             const struct attribute_group **groups);
473 static inline void add_disk(struct gendisk *disk)
474 {
475         device_add_disk(NULL, disk, NULL);
476 }
477 extern void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk);
478 static inline void add_disk_no_queue_reg(struct gendisk *disk)
479 {
480         device_add_disk_no_queue_reg(NULL, disk);
481 }
482
483 extern void del_gendisk(struct gendisk *gp);
484 extern struct gendisk *get_gendisk(dev_t dev, int *partno);
485 extern struct block_device *bdget_disk(struct gendisk *disk, int partno);
486
487 extern void set_device_ro(struct block_device *bdev, int flag);
488 extern void set_disk_ro(struct gendisk *disk, int flag);
489
490 static inline int get_disk_ro(struct gendisk *disk)
491 {
492         return disk->part0.policy;
493 }
494
495 extern void disk_block_events(struct gendisk *disk);
496 extern void disk_unblock_events(struct gendisk *disk);
497 extern void disk_flush_events(struct gendisk *disk, unsigned int mask);
498 extern void set_capacity_revalidate_and_notify(struct gendisk *disk,
499                         sector_t size, bool revalidate);
500 extern unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask);
501
502 /* drivers/char/random.c */
503 extern void add_disk_randomness(struct gendisk *disk) __latent_entropy;
504 extern void rand_initialize_disk(struct gendisk *disk);
505
506 static inline sector_t get_start_sect(struct block_device *bdev)
507 {
508         return bdev->bd_part->start_sect;
509 }
510 static inline sector_t get_capacity(struct gendisk *disk)
511 {
512         return disk->part0.nr_sects;
513 }
514 static inline void set_capacity(struct gendisk *disk, sector_t size)
515 {
516         disk->part0.nr_sects = size;
517 }
518
519 #ifdef CONFIG_SOLARIS_X86_PARTITION
520
521 #define SOLARIS_X86_NUMSLICE    16
522 #define SOLARIS_X86_VTOC_SANE   (0x600DDEEEUL)
523
524 struct solaris_x86_slice {
525         __le16 s_tag;           /* ID tag of partition */
526         __le16 s_flag;          /* permission flags */
527         __le32 s_start;         /* start sector no of partition */
528         __le32 s_size;          /* # of blocks in partition */
529 };
530
531 struct solaris_x86_vtoc {
532         unsigned int v_bootinfo[3];     /* info needed by mboot (unsupported) */
533         __le32 v_sanity;                /* to verify vtoc sanity */
534         __le32 v_version;               /* layout version */
535         char    v_volume[8];            /* volume name */
536         __le16  v_sectorsz;             /* sector size in bytes */
537         __le16  v_nparts;               /* number of partitions */
538         unsigned int v_reserved[10];    /* free space */
539         struct solaris_x86_slice
540                 v_slice[SOLARIS_X86_NUMSLICE]; /* slice headers */
541         unsigned int timestamp[SOLARIS_X86_NUMSLICE]; /* timestamp (unsupported) */
542         char    v_asciilabel[128];      /* for compatibility */
543 };
544
545 #endif /* CONFIG_SOLARIS_X86_PARTITION */
546
547 #ifdef CONFIG_BSD_DISKLABEL
548 /*
549  * BSD disklabel support by Yossi Gottlieb <yogo@math.tau.ac.il>
550  * updated by Marc Espie <Marc.Espie@openbsd.org>
551  */
552
553 /* check against BSD src/sys/sys/disklabel.h for consistency */
554
555 #define BSD_DISKMAGIC   (0x82564557UL)  /* The disk magic number */
556 #define BSD_MAXPARTITIONS       16
557 #define OPENBSD_MAXPARTITIONS   16
558 #define BSD_FS_UNUSED           0       /* disklabel unused partition entry ID */
559 struct bsd_disklabel {
560         __le32  d_magic;                /* the magic number */
561         __s16   d_type;                 /* drive type */
562         __s16   d_subtype;              /* controller/d_type specific */
563         char    d_typename[16];         /* type name, e.g. "eagle" */
564         char    d_packname[16];                 /* pack identifier */ 
565         __u32   d_secsize;              /* # of bytes per sector */
566         __u32   d_nsectors;             /* # of data sectors per track */
567         __u32   d_ntracks;              /* # of tracks per cylinder */
568         __u32   d_ncylinders;           /* # of data cylinders per unit */
569         __u32   d_secpercyl;            /* # of data sectors per cylinder */
570         __u32   d_secperunit;           /* # of data sectors per unit */
571         __u16   d_sparespertrack;       /* # of spare sectors per track */
572         __u16   d_sparespercyl;         /* # of spare sectors per cylinder */
573         __u32   d_acylinders;           /* # of alt. cylinders per unit */
574         __u16   d_rpm;                  /* rotational speed */
575         __u16   d_interleave;           /* hardware sector interleave */
576         __u16   d_trackskew;            /* sector 0 skew, per track */
577         __u16   d_cylskew;              /* sector 0 skew, per cylinder */
578         __u32   d_headswitch;           /* head switch time, usec */
579         __u32   d_trkseek;              /* track-to-track seek, usec */
580         __u32   d_flags;                /* generic flags */
581 #define NDDATA 5
582         __u32   d_drivedata[NDDATA];    /* drive-type specific information */
583 #define NSPARE 5
584         __u32   d_spare[NSPARE];        /* reserved for future use */
585         __le32  d_magic2;               /* the magic number (again) */
586         __le16  d_checksum;             /* xor of data incl. partitions */
587
588                         /* filesystem and partition information: */
589         __le16  d_npartitions;          /* number of partitions in following */
590         __le32  d_bbsize;               /* size of boot area at sn0, bytes */
591         __le32  d_sbsize;               /* max size of fs superblock, bytes */
592         struct  bsd_partition {         /* the partition table */
593                 __le32  p_size;         /* number of sectors in partition */
594                 __le32  p_offset;       /* starting sector */
595                 __le32  p_fsize;        /* filesystem basic fragment size */
596                 __u8    p_fstype;       /* filesystem type, see below */
597                 __u8    p_frag;         /* filesystem fragments per block */
598                 __le16  p_cpg;          /* filesystem cylinders per group */
599         } d_partitions[BSD_MAXPARTITIONS];      /* actually may be more */
600 };
601
602 #endif  /* CONFIG_BSD_DISKLABEL */
603
604 #ifdef CONFIG_UNIXWARE_DISKLABEL
605 /*
606  * Unixware slices support by Andrzej Krzysztofowicz <ankry@mif.pg.gda.pl>
607  * and Krzysztof G. Baranowski <kgb@knm.org.pl>
608  */
609
610 #define UNIXWARE_DISKMAGIC     (0xCA5E600DUL)   /* The disk magic number */
611 #define UNIXWARE_DISKMAGIC2    (0x600DDEEEUL)   /* The slice table magic nr */
612 #define UNIXWARE_NUMSLICE      16
613 #define UNIXWARE_FS_UNUSED     0                /* Unused slice entry ID */
614
615 struct unixware_slice {
616         __le16   s_label;       /* label */
617         __le16   s_flags;       /* permission flags */
618         __le32   start_sect;    /* starting sector */
619         __le32   nr_sects;      /* number of sectors in slice */
620 };
621
622 struct unixware_disklabel {
623         __le32   d_type;                /* drive type */
624         __le32   d_magic;                /* the magic number */
625         __le32   d_version;              /* version number */
626         char    d_serial[12];           /* serial number of the device */
627         __le32   d_ncylinders;           /* # of data cylinders per device */
628         __le32   d_ntracks;              /* # of tracks per cylinder */
629         __le32   d_nsectors;             /* # of data sectors per track */
630         __le32   d_secsize;              /* # of bytes per sector */
631         __le32   d_part_start;           /* # of first sector of this partition */
632         __le32   d_unknown1[12];         /* ? */
633         __le32  d_alt_tbl;              /* byte offset of alternate table */
634         __le32  d_alt_len;              /* byte length of alternate table */
635         __le32  d_phys_cyl;             /* # of physical cylinders per device */
636         __le32  d_phys_trk;             /* # of physical tracks per cylinder */
637         __le32  d_phys_sec;             /* # of physical sectors per track */
638         __le32  d_phys_bytes;           /* # of physical bytes per sector */
639         __le32  d_unknown2;             /* ? */
640         __le32   d_unknown3;             /* ? */
641         __le32  d_pad[8];               /* pad */
642
643         struct unixware_vtoc {
644                 __le32  v_magic;                /* the magic number */
645                 __le32  v_version;              /* version number */
646                 char    v_name[8];              /* volume name */
647                 __le16  v_nslices;              /* # of slices */
648                 __le16  v_unknown1;             /* ? */
649                 __le32  v_reserved[10];         /* reserved */
650                 struct unixware_slice
651                         v_slice[UNIXWARE_NUMSLICE];     /* slice headers */
652         } vtoc;
653
654 };  /* 408 */
655
656 #endif /* CONFIG_UNIXWARE_DISKLABEL */
657
658 #ifdef CONFIG_MINIX_SUBPARTITION
659 #   define MINIX_NR_SUBPARTITIONS  4
660 #endif /* CONFIG_MINIX_SUBPARTITION */
661
662 #define ADDPART_FLAG_NONE       0
663 #define ADDPART_FLAG_RAID       1
664 #define ADDPART_FLAG_WHOLEDISK  2
665
666 extern int blk_alloc_devt(struct hd_struct *part, dev_t *devt);
667 extern void blk_free_devt(dev_t devt);
668 extern void blk_invalidate_devt(dev_t devt);
669 extern dev_t blk_lookup_devt(const char *name, int partno);
670 extern char *disk_name (struct gendisk *hd, int partno, char *buf);
671
672 int bdev_disk_changed(struct block_device *bdev, bool invalidate);
673 int blk_add_partitions(struct gendisk *disk, struct block_device *bdev);
674 int blk_drop_partitions(struct gendisk *disk, struct block_device *bdev);
675 extern int disk_expand_part_tbl(struct gendisk *disk, int target);
676 extern struct hd_struct * __must_check add_partition(struct gendisk *disk,
677                                                      int partno, sector_t start,
678                                                      sector_t len, int flags,
679                                                      struct partition_meta_info
680                                                        *info);
681 extern void __delete_partition(struct percpu_ref *);
682 extern void delete_partition(struct gendisk *, int);
683 extern void printk_all_partitions(void);
684
685 extern struct gendisk *__alloc_disk_node(int minors, int node_id);
686 extern struct kobject *get_disk_and_module(struct gendisk *disk);
687 extern void put_disk(struct gendisk *disk);
688 extern void put_disk_and_module(struct gendisk *disk);
689 extern void blk_register_region(dev_t devt, unsigned long range,
690                         struct module *module,
691                         struct kobject *(*probe)(dev_t, int *, void *),
692                         int (*lock)(dev_t, void *),
693                         void *data);
694 extern void blk_unregister_region(dev_t devt, unsigned long range);
695
696 #define alloc_disk_node(minors, node_id)                                \
697 ({                                                                      \
698         static struct lock_class_key __key;                             \
699         const char *__name;                                             \
700         struct gendisk *__disk;                                         \
701                                                                         \
702         __name = "(gendisk_completion)"#minors"("#node_id")";           \
703                                                                         \
704         __disk = __alloc_disk_node(minors, node_id);                    \
705                                                                         \
706         if (__disk)                                                     \
707                 lockdep_init_map(&__disk->lockdep_map, __name, &__key, 0); \
708                                                                         \
709         __disk;                                                         \
710 })
711
712 #define alloc_disk(minors) alloc_disk_node(minors, NUMA_NO_NODE)
713
714 static inline int hd_ref_init(struct hd_struct *part)
715 {
716         if (percpu_ref_init(&part->ref, __delete_partition, 0,
717                                 GFP_KERNEL))
718                 return -ENOMEM;
719         return 0;
720 }
721
722 static inline void hd_struct_get(struct hd_struct *part)
723 {
724         percpu_ref_get(&part->ref);
725 }
726
727 static inline int hd_struct_try_get(struct hd_struct *part)
728 {
729         return percpu_ref_tryget_live(&part->ref);
730 }
731
732 static inline void hd_struct_put(struct hd_struct *part)
733 {
734         percpu_ref_put(&part->ref);
735 }
736
737 static inline void hd_struct_kill(struct hd_struct *part)
738 {
739         percpu_ref_kill(&part->ref);
740 }
741
742 static inline void hd_free_part(struct hd_struct *part)
743 {
744         free_part_stats(part);
745         kfree(part->info);
746         percpu_ref_exit(&part->ref);
747 }
748
749 /*
750  * Any access of part->nr_sects which is not protected by partition
751  * bd_mutex or gendisk bdev bd_mutex, should be done using this
752  * accessor function.
753  *
754  * Code written along the lines of i_size_read() and i_size_write().
755  * CONFIG_PREEMPTION case optimizes the case of UP kernel with preemption
756  * on.
757  */
758 static inline sector_t part_nr_sects_read(struct hd_struct *part)
759 {
760 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
761         sector_t nr_sects;
762         unsigned seq;
763         do {
764                 seq = read_seqcount_begin(&part->nr_sects_seq);
765                 nr_sects = part->nr_sects;
766         } while (read_seqcount_retry(&part->nr_sects_seq, seq));
767         return nr_sects;
768 #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
769         sector_t nr_sects;
770
771         preempt_disable();
772         nr_sects = part->nr_sects;
773         preempt_enable();
774         return nr_sects;
775 #else
776         return part->nr_sects;
777 #endif
778 }
779
780 /*
781  * Should be called with mutex lock held (typically bd_mutex) of partition
782  * to provide mutual exlusion among writers otherwise seqcount might be
783  * left in wrong state leaving the readers spinning infinitely.
784  */
785 static inline void part_nr_sects_write(struct hd_struct *part, sector_t size)
786 {
787 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
788         write_seqcount_begin(&part->nr_sects_seq);
789         part->nr_sects = size;
790         write_seqcount_end(&part->nr_sects_seq);
791 #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
792         preempt_disable();
793         part->nr_sects = size;
794         preempt_enable();
795 #else
796         part->nr_sects = size;
797 #endif
798 }
799
800 #if defined(CONFIG_BLK_DEV_INTEGRITY)
801 extern void blk_integrity_add(struct gendisk *);
802 extern void blk_integrity_del(struct gendisk *);
803 #else   /* CONFIG_BLK_DEV_INTEGRITY */
804 static inline void blk_integrity_add(struct gendisk *disk) { }
805 static inline void blk_integrity_del(struct gendisk *disk) { }
806 #endif  /* CONFIG_BLK_DEV_INTEGRITY */
807
808 #else /* CONFIG_BLOCK */
809
810 static inline void printk_all_partitions(void) { }
811
812 static inline dev_t blk_lookup_devt(const char *name, int partno)
813 {
814         dev_t devt = MKDEV(0, 0);
815         return devt;
816 }
817 #endif /* CONFIG_BLOCK */
818
819 #endif /* _LINUX_GENHD_H */