172 lines · 3.1 KB · Text
RawDownload
.TH DISK 2.SH NAMEopendisk, Disk \- generic disk device interface.SH SYNOPSIS.nf.ft L#include <u.h>#include <libc.h>#include <disk.h>.ft.PP.ft Ltypedef struct Disk {	char *prefix;	char part[NAMELEN];	int fd, wfd, ctlfd, rdonly;	int type;	vlong secs, secsize, size, offset;	int c, h, s;} Disk;.ft.PP.BDisk* opendisk(char *file, int rdonly, int noctl).SH DESCRIPTIONThese routines provide a simple way to gatherand use information about .IR floppy (3)and.IR sd (3)disks and disk partitions,as well as plain files..PP.I Opendiskopens.I filefor reading and stores the file descriptor inthe .B fdfield of the.B Diskstructure.If .I rdonlyis not set, .I opendiskalso opens.I filefor writing and stores that file descriptor in.BR wfd .The two file descriptors are kept separate tohelp prevent accidents..PPIf.I noctlis not set, .I opendisklooks for a.B ctlfile in the same directory as thedisk file;if it finds one, it declaresthe disk to bean.I sddevice,setting the.B typefield in the.B Diskstructureto.BR Tsd .If the passed.I fileis named.BI fd n disk \fR,it looks for a file.BI fd n ctl \fR,and if it finds that,declares the disk to bea floppy disk, of type.BR Tfloppy .If eithercontrolfile is found, it is opened for readingand writing, and the resulting file descriptoris saved as .BR ctlfd .Otherwise the returned diskhas type.BR Tfile ..PP.I Opendiskthen stats the file and stores its length in.BR size .If the disk is an.I sdpartition, .I opendiskreads the sector size from thecontrolfile and stores it in .BR secsize ;otherwise the sector size is assumed to be 512,as is the case for floppy disks..I Opendiskthen stores the disk size measured in sectors in.BR secs ..PPIf the disk is an.I sdpartition, .I opendisk parses thecontrolfile to find the partition's offsetwithin its disk;otherwise it sets.B offsetto zero.If the disk is an ATA disk,.I opendiskreadsthe disk geometry (number of cylinders, heads, and sectors)from the .B geometryline in the.I sdcontrol file;otherwise it sets these to zero as well..B Nameis initialized with the base name ofthe disk partition, and is useful for forming messages to the.I sdcontrol file..B Prefixis set to the passed filename withoutthe .B namesuffix..PPThe IBM PC BIOS interface allocates10 bits for the number of cylinders, 8 for the number of heads, and 6 for the number of sectors per track.Disk geometries are not quite so simpleanymore, but to keep the interface useful,modern disks and BIOSes present geometriesthat still fit within these constraints.These numbers are still used when partitioningand formatting disks..I Opendiskemploys a number of heuristics to discover thissupposed geometry and store it in the.BR c ,.BR h ,and.B sfields.Disk offsets in partition tables andin FAT descriptors are stored in a formdependent upon these numbers, so.I opendiskworks hard to report numbers thatagree with those used by other operatingsystems; the numbers bear little or no resemblanceto reality..SH SOURCE.B /sys/src/libdisk/disk.c.SH SEE ALSO.IR floppy (3),.IR sd (3)