ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / linux / genhd.h
1 #ifndef _LINUX_GENHD_H
2 #define _LINUX_GENHD_H
3
4 /*
5  *      genhd.h Copyright (C) 1992 Drew Eckhardt
6  *      Generic hard disk header file by  
7  *              Drew Eckhardt
8  *
9  *              <drew@colorado.edu>
10  */
11
12 #include <linux/config.h>
13 #include <linux/types.h>
14 #include <linux/major.h>
15 #include <linux/device.h>
16 #include <linux/smp.h>
17 #include <linux/string.h>
18 #include <linux/fs.h>
19
20 enum {
21 /* These three have identical behaviour; use the second one if DOS FDISK gets
22    confused about extended/logical partitions starting past cylinder 1023. */
23         DOS_EXTENDED_PARTITION = 5,
24         LINUX_EXTENDED_PARTITION = 0x85,
25         WIN98_EXTENDED_PARTITION = 0x0f,
26
27         LINUX_SWAP_PARTITION = 0x82,
28         LINUX_RAID_PARTITION = 0xfd,    /* autodetect RAID partition */
29
30         SOLARIS_X86_PARTITION = LINUX_SWAP_PARTITION,
31
32         DM6_AUX1PARTITION = 0x51,       /* no DDO:  use xlated geom */
33         DM6_AUX3PARTITION = 0x53,       /* no DDO:  use xlated geom */
34         DM6_PARTITION = 0x54,           /* has DDO: use xlated geom & offset */
35         EZD_PARTITION = 0x55,           /* EZ-DRIVE */
36
37         FREEBSD_PARTITION = 0xa5,       /* FreeBSD Partition ID */
38         OPENBSD_PARTITION = 0xa6,       /* OpenBSD Partition ID */
39         NETBSD_PARTITION = 0xa9,        /* NetBSD Partition ID */
40         BSDI_PARTITION = 0xb7,          /* BSDI Partition ID */
41         MINIX_PARTITION = 0x81,         /* Minix Partition ID */
42         UNIXWARE_PARTITION = 0x63,      /* Same as GNU_HURD and SCO Unix */
43 };
44
45 struct partition {
46         unsigned char boot_ind;         /* 0x80 - active */
47         unsigned char head;             /* starting head */
48         unsigned char sector;           /* starting sector */
49         unsigned char cyl;              /* starting cylinder */
50         unsigned char sys_ind;          /* What partition type */
51         unsigned char end_head;         /* end head */
52         unsigned char end_sector;       /* end sector */
53         unsigned char end_cyl;          /* end cylinder */
54         unsigned int start_sect;        /* starting sector counting from 0 */
55         unsigned int nr_sects;          /* nr of sectors in partition */
56 } __attribute__((packed));
57
58 #ifdef __KERNEL__
59 struct hd_struct {
60         sector_t start_sect;
61         sector_t nr_sects;
62         struct kobject kobj;
63         unsigned reads, read_sectors, writes, write_sectors;
64         int policy, partno;
65 };
66
67 #define GENHD_FL_REMOVABLE                      1
68 #define GENHD_FL_DRIVERFS                       2
69 #define GENHD_FL_CD                             8
70 #define GENHD_FL_UP                             16
71 #define GENHD_FL_SUPPRESS_PARTITION_INFO        32
72
73 struct disk_stats {
74         unsigned read_sectors, write_sectors;
75         unsigned reads, writes;
76         unsigned read_merges, write_merges;
77         unsigned read_ticks, write_ticks;
78         unsigned io_ticks;
79         unsigned time_in_queue;
80 };
81         
82 struct gendisk {
83         int major;                      /* major number of driver */
84         int first_minor;
85         int minors;
86         char disk_name[32];             /* name of major driver */
87         struct hd_struct **part;        /* [indexed by minor] */
88         struct block_device_operations *fops;
89         struct request_queue *queue;
90         void *private_data;
91         sector_t capacity;
92
93         int flags;
94         char devfs_name[64];            /* devfs crap */
95         int number;                     /* more of the same */
96         struct device *driverfs_dev;
97         struct kobject kobj;
98
99         struct timer_rand_state *random;
100         int policy;
101
102         unsigned sync_io;               /* RAID */
103         unsigned long stamp, stamp_idle;
104         int in_flight;
105 #ifdef  CONFIG_SMP
106         struct disk_stats *dkstats;
107 #else
108         struct disk_stats dkstats;
109 #endif
110 };
111
112 /* 
113  * Macros to operate on percpu disk statistics:
114  * Since writes to disk_stats are serialised through the queue_lock,
115  * smp_processor_id() should be enough to get to the per_cpu versions
116  * of statistics counters
117  */
118 #ifdef  CONFIG_SMP
119 #define disk_stat_add(gendiskp, field, addnd)   \
120         (per_cpu_ptr(gendiskp->dkstats, smp_processor_id())->field += addnd)
121 #define disk_stat_read(gendiskp, field)                                 \
122 ({                                                                      \
123         typeof(gendiskp->dkstats->field) res = 0;                       \
124         int i;                                                          \
125         for (i=0; i < NR_CPUS; i++) {                                   \
126                 if (!cpu_possible(i))                                   \
127                         continue;                                       \
128                 res += per_cpu_ptr(gendiskp->dkstats, i)->field;        \
129         }                                                               \
130         res;                                                            \
131 })
132
133 static inline void disk_stat_set_all(struct gendisk *gendiskp, int value)       {
134         int i;
135         for (i=0; i < NR_CPUS; i++) {
136                 if (cpu_possible(i)) {
137                         memset(per_cpu_ptr(gendiskp->dkstats, i), value,        
138                                         sizeof (struct disk_stats));
139                 }
140         }
141 }               
142                                 
143 #else
144 #define disk_stat_add(gendiskp, field, addnd) (gendiskp->dkstats.field += addnd)
145 #define disk_stat_read(gendiskp, field) (gendiskp->dkstats.field)
146
147 static inline void disk_stat_set_all(struct gendisk *gendiskp, int value)       {
148         memset(&gendiskp->dkstats, value, sizeof (struct disk_stats));
149 }
150 #endif
151
152 #define disk_stat_inc(gendiskp, field) disk_stat_add(gendiskp, field, 1)
153 #define disk_stat_dec(gendiskp, field) disk_stat_add(gendiskp, field, -1)
154 #define disk_stat_sub(gendiskp, field, subnd) \
155                 disk_stat_add(gendiskp, field, -subnd)
156
157
158 /* Inlines to alloc and free disk stats in struct gendisk */
159 #ifdef  CONFIG_SMP
160 static inline int init_disk_stats(struct gendisk *disk)
161 {
162         disk->dkstats = alloc_percpu(struct disk_stats);
163         if (!disk->dkstats)
164                 return 0;
165         return 1;
166 }
167
168 static inline void free_disk_stats(struct gendisk *disk)
169 {
170         free_percpu(disk->dkstats);
171 }
172 #else   /* CONFIG_SMP */
173 static inline int init_disk_stats(struct gendisk *disk)
174 {
175         return 1;
176 }
177
178 static inline void free_disk_stats(struct gendisk *disk)
179 {
180 }
181 #endif  /* CONFIG_SMP */
182
183 /* drivers/block/ll_rw_blk.c */
184 extern void disk_round_stats(struct gendisk *disk);
185
186 /* drivers/block/genhd.c */
187 extern void add_disk(struct gendisk *disk);
188 extern void del_gendisk(struct gendisk *gp);
189 extern void unlink_gendisk(struct gendisk *gp);
190 extern struct gendisk *get_gendisk(dev_t dev, int *part);
191
192 extern void set_device_ro(struct block_device *bdev, int flag);
193 extern void set_disk_ro(struct gendisk *disk, int flag);
194
195 /* drivers/char/random.c */
196 extern void add_disk_randomness(struct gendisk *disk);
197 extern void rand_initialize_disk(struct gendisk *disk);
198
199 static inline sector_t get_start_sect(struct block_device *bdev)
200 {
201         return bdev->bd_contains == bdev ? 0 : bdev->bd_part->start_sect;
202 }
203 static inline sector_t get_capacity(struct gendisk *disk)
204 {
205         return disk->capacity;
206 }
207 static inline void set_capacity(struct gendisk *disk, sector_t size)
208 {
209         disk->capacity = size;
210 }
211
212 #endif  /*  __KERNEL__  */
213
214 #ifdef CONFIG_SOLARIS_X86_PARTITION
215
216 #define SOLARIS_X86_NUMSLICE    8
217 #define SOLARIS_X86_VTOC_SANE   (0x600DDEEEUL)
218
219 struct solaris_x86_slice {
220         ushort  s_tag;                  /* ID tag of partition */
221         ushort  s_flag;                 /* permission flags */
222         unsigned int s_start;           /* start sector no of partition */
223         unsigned int s_size;            /* # of blocks in partition */
224 };
225
226 struct solaris_x86_vtoc {
227         unsigned int v_bootinfo[3];     /* info needed by mboot (unsupported) */
228         unsigned int v_sanity;          /* to verify vtoc sanity */
229         unsigned int v_version;         /* layout version */
230         char    v_volume[8];            /* volume name */
231         ushort  v_sectorsz;             /* sector size in bytes */
232         ushort  v_nparts;               /* number of partitions */
233         unsigned int v_reserved[10];    /* free space */
234         struct solaris_x86_slice
235                 v_slice[SOLARIS_X86_NUMSLICE]; /* slice headers */
236         unsigned int timestamp[SOLARIS_X86_NUMSLICE]; /* timestamp (unsupported) */
237         char    v_asciilabel[128];      /* for compatibility */
238 };
239
240 #endif /* CONFIG_SOLARIS_X86_PARTITION */
241
242 #ifdef CONFIG_BSD_DISKLABEL
243 /*
244  * BSD disklabel support by Yossi Gottlieb <yogo@math.tau.ac.il>
245  * updated by Marc Espie <Marc.Espie@openbsd.org>
246  */
247
248 /* check against BSD src/sys/sys/disklabel.h for consistency */
249
250 #define BSD_DISKMAGIC   (0x82564557UL)  /* The disk magic number */
251 #define BSD_MAXPARTITIONS       8
252 #define OPENBSD_MAXPARTITIONS   16
253 #define BSD_FS_UNUSED           0       /* disklabel unused partition entry ID */
254 struct bsd_disklabel {
255         __u32   d_magic;                /* the magic number */
256         __s16   d_type;                 /* drive type */
257         __s16   d_subtype;              /* controller/d_type specific */
258         char    d_typename[16];         /* type name, e.g. "eagle" */
259         char    d_packname[16];                 /* pack identifier */ 
260         __u32   d_secsize;              /* # of bytes per sector */
261         __u32   d_nsectors;             /* # of data sectors per track */
262         __u32   d_ntracks;              /* # of tracks per cylinder */
263         __u32   d_ncylinders;           /* # of data cylinders per unit */
264         __u32   d_secpercyl;            /* # of data sectors per cylinder */
265         __u32   d_secperunit;           /* # of data sectors per unit */
266         __u16   d_sparespertrack;       /* # of spare sectors per track */
267         __u16   d_sparespercyl;         /* # of spare sectors per cylinder */
268         __u32   d_acylinders;           /* # of alt. cylinders per unit */
269         __u16   d_rpm;                  /* rotational speed */
270         __u16   d_interleave;           /* hardware sector interleave */
271         __u16   d_trackskew;            /* sector 0 skew, per track */
272         __u16   d_cylskew;              /* sector 0 skew, per cylinder */
273         __u32   d_headswitch;           /* head switch time, usec */
274         __u32   d_trkseek;              /* track-to-track seek, usec */
275         __u32   d_flags;                /* generic flags */
276 #define NDDATA 5
277         __u32   d_drivedata[NDDATA];    /* drive-type specific information */
278 #define NSPARE 5
279         __u32   d_spare[NSPARE];        /* reserved for future use */
280         __u32   d_magic2;               /* the magic number (again) */
281         __u16   d_checksum;             /* xor of data incl. partitions */
282
283                         /* filesystem and partition information: */
284         __u16   d_npartitions;          /* number of partitions in following */
285         __u32   d_bbsize;               /* size of boot area at sn0, bytes */
286         __u32   d_sbsize;               /* max size of fs superblock, bytes */
287         struct  bsd_partition {         /* the partition table */
288                 __u32   p_size;         /* number of sectors in partition */
289                 __u32   p_offset;       /* starting sector */
290                 __u32   p_fsize;        /* filesystem basic fragment size */
291                 __u8    p_fstype;       /* filesystem type, see below */
292                 __u8    p_frag;         /* filesystem fragments per block */
293                 __u16   p_cpg;          /* filesystem cylinders per group */
294         } d_partitions[BSD_MAXPARTITIONS];      /* actually may be more */
295 };
296
297 #endif  /* CONFIG_BSD_DISKLABEL */
298
299 #ifdef CONFIG_UNIXWARE_DISKLABEL
300 /*
301  * Unixware slices support by Andrzej Krzysztofowicz <ankry@mif.pg.gda.pl>
302  * and Krzysztof G. Baranowski <kgb@knm.org.pl>
303  */
304
305 #define UNIXWARE_DISKMAGIC     (0xCA5E600DUL)   /* The disk magic number */
306 #define UNIXWARE_DISKMAGIC2    (0x600DDEEEUL)   /* The slice table magic nr */
307 #define UNIXWARE_NUMSLICE      16
308 #define UNIXWARE_FS_UNUSED     0                /* Unused slice entry ID */
309
310 struct unixware_slice {
311         __u16   s_label;        /* label */
312         __u16   s_flags;        /* permission flags */
313         __u32   start_sect;     /* starting sector */
314         __u32   nr_sects;       /* number of sectors in slice */
315 };
316
317 struct unixware_disklabel {
318         __u32   d_type;                 /* drive type */
319         __u32   d_magic;                /* the magic number */
320         __u32   d_version;              /* version number */
321         char    d_serial[12];           /* serial number of the device */
322         __u32   d_ncylinders;           /* # of data cylinders per device */
323         __u32   d_ntracks;              /* # of tracks per cylinder */
324         __u32   d_nsectors;             /* # of data sectors per track */
325         __u32   d_secsize;              /* # of bytes per sector */
326         __u32   d_part_start;           /* # of first sector of this partition */
327         __u32   d_unknown1[12];         /* ? */
328         __u32   d_alt_tbl;              /* byte offset of alternate table */
329         __u32   d_alt_len;              /* byte length of alternate table */
330         __u32   d_phys_cyl;             /* # of physical cylinders per device */
331         __u32   d_phys_trk;             /* # of physical tracks per cylinder */
332         __u32   d_phys_sec;             /* # of physical sectors per track */
333         __u32   d_phys_bytes;           /* # of physical bytes per sector */
334         __u32   d_unknown2;             /* ? */
335         __u32   d_unknown3;             /* ? */
336         __u32   d_pad[8];               /* pad */
337
338         struct unixware_vtoc {
339                 __u32   v_magic;                /* the magic number */
340                 __u32   v_version;              /* version number */
341                 char    v_name[8];              /* volume name */
342                 __u16   v_nslices;              /* # of slices */
343                 __u16   v_unknown1;             /* ? */
344                 __u32   v_reserved[10];         /* reserved */
345                 struct unixware_slice
346                         v_slice[UNIXWARE_NUMSLICE];     /* slice headers */
347         } vtoc;
348
349 };  /* 408 */
350
351 #endif /* CONFIG_UNIXWARE_DISKLABEL */
352
353 #ifdef CONFIG_MINIX_SUBPARTITION
354 #   define MINIX_NR_SUBPARTITIONS  4
355 #endif /* CONFIG_MINIX_SUBPARTITION */
356
357 #ifdef __KERNEL__
358
359 char *disk_name (struct gendisk *hd, int part, char *buf);
360
361 extern int rescan_partitions(struct gendisk *disk, struct block_device *bdev);
362 extern void add_partition(struct gendisk *, int, sector_t, sector_t);
363 extern void delete_partition(struct gendisk *, int);
364
365 extern struct gendisk *alloc_disk(int minors);
366 extern struct kobject *get_disk(struct gendisk *disk);
367 extern void put_disk(struct gendisk *disk);
368
369 extern void blk_register_region(dev_t dev, unsigned long range,
370                         struct module *module,
371                         struct kobject *(*probe)(dev_t, int *, void *),
372                         int (*lock)(dev_t, void *),
373                         void *data);
374 extern void blk_unregister_region(dev_t dev, unsigned long range);
375
376 static inline struct block_device *bdget_disk(struct gendisk *disk, int index)
377 {
378         return bdget(MKDEV(disk->major, disk->first_minor) + index);
379 }
380
381 #endif
382
383 #endif