Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Device file

From Wikipedia, the free encyclopedia
(Redirected fromBlock device)
Interface to a device driver that appears in a file system as if it were an ordinary file
Not to be confused withDevice tree.

InUnix-likeoperating systems, adevice file,device node, orspecial file is aninterface to adevice driver that appears in afile system as if it were an ordinaryfile. There are also special files inDOS,OS/2, andWindows. These special files allow an application program to interact with a device by using its device driver via standardinput/outputsystem calls. Using standard system calls simplifies many programming tasks, and leads to consistent user-space I/O mechanisms regardless of device features and functions.

Overview

[edit]

Device files usually provide simple interfaces to standard devices (such as printers and serial ports), but can also be used to access specific unique resources on those devices, such asdisk partitions. Additionally, device files are useful for accessingsystem resources that have no connection with any actual device, such asdata sinks andrandom number generators.

There are two general kinds of device files in Unix-like operating systems, known ascharacter special files andblock special files. The difference between them lies in how much data is read and written by the operating system and hardware. These together can be calleddevice special files in contrast tonamed pipes, which are not connected to a device but are not ordinary files either.

MS-DOS borrowed the concept of special files from Unix but renamed themdevices.[1] Because early versions of MS-DOS did not support adirectory hierarchy, devices were distinguished from regular files by making their namesreserved words that cannot be used as folder or file names; for example: the wordCON is a reserved word. These were chosen for a degree of compatibility withCP/M and are still present in modern Windows for backwards compatibility. Names are not case-sensitive, so "con", "Con", and "CON" are all invalid names.

InWindows XP, entering "Con" into theRun command returns the error message, "This file does not have a program associated with it for performing this action. Create an association in the Folder Options control panel." Attempting to rename any file or folder using a reserved name silently reverts the file or folder to its previous name (or "New Folder", "New Text Document", etc.), with no notification or error message.[2] InWindows Vista and later, attempting to use a reserved name for a file or folder brings up an error message saying, "The specified device name is invalid."[2]

In some Unix-like systems, most device files are managed as part of avirtual file system traditionally mounted at/dev, possibly associated with a controlling daemon, which monitors hardware addition and removal at run time, making corresponding changes to the device file system if that's not automatically done by the kernel, and possibly invoking scripts in system or user space to handle special device needs. TheFreeBSD,DragonFly BSD andDarwin have a dedicated file systemdevfs; device nodes are managed automatically by this file system, inkernel space. Linux used to have a similardevfs implementation, but it was abandoned later, and then removed since version 2.6.17;[3] Linux now primarily uses auser space implementation known asudev, but there are many variants.

In Unix systems which supportchroot process isolation, such asSolaris Containers, typically each chroot environment needs its own/dev; these mount points will be visible on the host OS at various nodes in the global file system tree. By restricting the device nodes populated into chroot instances of/dev, hardware isolation can be enforced by the chroot environment (a program can not meddle with hardware that it can neither see nor name—an even stronger form ofaccess control than Unixfile system permissions).

MS-DOS managed hardware device contention (seeterminate-and-stay-resident program) by making each device file exclusive open. An application attempting to access a device already in use would discover itself unable to open the device file node. A variety ofdevice driver semantics are implemented in Unix and Linux concerningconcurrent access.[4]

Unix and Unix-like systems

[edit]
A simplified structure of the Linux kernel. File systems are implemented as part of the I/O subsystem.

Device nodes correspond to resources that an operating system'skernel has already allocated. Unix identifies those resources by amajor number and aminor number,[5] both stored as part of the structure of anode. The assignment of these numbers occurs uniquely in differentoperating systems and on differentcomputer platforms. Generally, the major number identifies the device driver and the minor number identifies a particular device (possibly out of many) that the driver controls:[6] in this case, the system may pass the minor number to a driver. However, in the presence ofdynamic number allocation, this may not be the case (e.g. onFreeBSD 5 and up).

As with other special file types, the computer system accesses device nodes using standard system calls and treats them like regular computer files. Two standard types of device files exist; unfortunately their names are rather counter-intuitive for historical reasons, and explanations of the difference between the two are often incorrect as a result.

Character devices

[edit]

Character special files orcharacter devices provide unbuffered, direct access to the hardware device. They do not necessarily allow programs to read or write single characters at a time; that is up to the device in question. The character device for a hard disk, for example, will normally require that all reads and writes be aligned to block boundaries and most certainly will not allow reading a single byte.

Character devices are sometimes known asraw devices to avoid the confusion surrounding the fact that a character device for a piece of block-based hardware will typically require programs to read and write aligned blocks.

Block devices

[edit]

Block special files orblock devices provide buffered access to hardware devices, and provide some abstraction from their specifics.[7] Unlike character devices, block devices will always allow the programmer to read or write a block of any size (including single characters/bytes) and any alignment. The downside is that because block devices are buffered, the programmer does not know how long it will take before written data is passed from the kernel's buffers to the actual device, or indeed in what order two separate writes will arrive at the physical device. Additionally, if the same hardware exposes both character and block devices, there is a risk of data corruption due to clients using the character device being unaware of changes made in the buffers of the block device.

Most systems create both block and character devices to represent hardware like hard disks. FreeBSD and Linux notably do not; the former has removed support for block devices,[8] while the latter creates only block devices. To get the effect of a character device from a block device on Linux, one must open the device with the Linux-specificO_DIRECT flag.

Pseudo-devices

[edit]

Device nodes on Unix-like systems do not necessarily have to correspond tophysical devices. Nodes that lack this correspondence are calledpseudo-devices. They provide various functions handled by the operating system. Some of the most commonly used (character-based) pseudo-devices include:

  • /dev/null – accepts and discards all input written to it; provides anend-of-file indication when read from.
  • /dev/zero – accepts and discards all input written to it; produces a continuous stream ofnull characters (zero-value bytes) as output when read from.
  • /dev/full – produces a continuous stream of null characters (zero-value bytes) as output when read from, and generates anENOSPC ("disk full") error when attempting to write to it.
  • /dev/random – produces bytes generated by the kernel'scryptographically secure pseudorandom number generator. Its exact behavior varies by implementation, and sometimes variants such as/dev/urandom or/dev/arandom are also provided.
  • /dev/stdin,/dev/stdout,/dev/stderr – access the process'sstandard streams.
  • /dev/fd/n – accesses the process'sfile descriptorn.

Additionally, BSD-specific pseudo-devices with anioctl interface may also include:

Node creation

[edit]

Nodes are created by themknodsystem call. The command-line program for creating nodes is also calledmknod. Nodes can be moved or deleted by the usual filesystem system calls (rename,unlink) andcommands (mv,rm).

Some Unix versions include a script namedmakedev orMAKEDEV to create all necessary devices in the directory/dev. It only makes sense on systems whose devices are statically assigned major numbers (e.g., by means of hardcoding it in their kernel module).

Some other Unix systems such asFreeBSD use kernel-based device node management via devfs only and do not support manual node creation.mknod(2) system call andmknod(8) command exist to keep compatibility with POSIX, but manually created device nodes outside devfs will not function at all.[10]

Naming conventions

[edit]

The following prefixes are used for the names of some devices in the/dev hierarchy, to identify the type of device:

Some additional prefixes have come into common use in some operating systems:

  • fb:frame buffer
  • fd: (platform)floppy disks, though this same abbreviation is also commonly used to refer tofile descriptor
  • hd: ("classic")IDE driver (previously used for ATAhard disk drive, ATAPIoptical disc drives, etc.)
    • hda: the primary device on the firstATA channel (usually identified by major number 3 and minor number 0)
    • hdb: the secondary device on the first ATA channel
    • hdc: the primary device on the second ATA channel
    • hdd: the secondary device on the second ATA channel
  • parport,pp:parallel ports
  • mem:Main memory (character device)
  • nbd:Network block device: Abstraction that represents block devices that are mounted through the network (or from images using qemu-nbd)
  • NVMe driver:
    • nvme0: first registered device's device controller (character device)
    • nvme0n1: first registered device's first namespace (block device)
    • nvme0n1p1: first registered device's first namespace's first partition (block device)
  • MMC driver:
    • mmcblk: storage driver forMMC media (SD cards, eMMC chips on laptops, etc.)
      • mmcblk0: first registered device
      • mmcblk0p1: first registered device's first partition
  • SCSI driver, also used bylibATA (modernPATA/SATA driver),USB,IEEE 1394, etc.:
    • sd: mass-storage driver (block device)
      • sda: first registered device
      • sdb, sdc, etc.: second, third, etc. registered devices
    • ses: Enclosure driver
    • sg: generic SCSI layer
    • sr: "ROM" driver (data-oriented optical disc drives; scd is just a secondary alias)
    • st:magnetic tape driver
  • tty:terminals
    • ttyS: (platform)serial port driver
    • ttyUSB: USB serial converters, modems, etc.

The canonical list of the prefixes used in Linux can be found in theLinux Device List, the official registry of allocated device numbers and/dev directory nodes for the Linux operating system.[11]

For most devices, this prefix is followed by a number uniquely identifying the particular device. For hard drives, a letter is used to identify devices and is followed by a number to identifypartitions. Thus a file system may "know" an area on a disk as/dev/sda3, for example, or "see" a networked terminal session as associated with/dev/pts/14.

On disks using the typical PCmaster boot record, the device numbers of primary and the optional extended partition are numbered 1 through 4, while the indexes of any logical partitions are 5 and onwards, regardless of the layout of the former partitions (their parent extended partition does not need to be the fourth partition on the disk, nor do all four primary partitions have to exist).

Device names are usually not portable between different Unix-like system variants, for example, on someBSD systems, the IDE devices are named/dev/wd0,/dev/wd1, etc.

devfs

[edit]

devfs is a specific implementation of a device file system on Unix-like operating systems, used for presenting device files. The underlying mechanism of implementation may vary, depending on the OS.

Maintaining these special files on a physically-implemented file system such as a hard drive is inconvenient, and as it needs kernel assistance anyway, the idea arose of a special-purpose logical file system that is not physically stored.

Defining when devices are ready to appear is not trivial. The devfs approach is for the device driver to request creation and deletion of devfs entries related to the devices it enables and disables.

PC DOS, TOS, OS/2, and Windows

[edit]

A device file is a reserved keyword used inPC DOS,TOS,OS/2, andWindows systems to allow access to certain ports and devices.

MS-DOS borrowed the concept of special files from Unix but renamed themdevices.[1] Because early versions of MS-DOS did not support adirectory hierarchy, devices were distinguished from regular files by making their namesreserved words. This means that certain file names were reserved for devices, and should not be used to name new files or directories.[12]The reserved names themselves were chosen to be compatible with "special files" handling ofPIP command inCP/M. There were two kinds of devices in DOS: Block Devices (used for disk drives) and Character Devices (generally all other devices, including COM and PRN devices).[13]

DOS uses device files for accessing printers and ports. Most versions of Windows also contain this support, which can cause confusion when trying to make files and folders of certain names, as they cannot have these names.[14] Versions 2.x ofMS-DOS provide theAVAILDEVCONFIG.SYS parameter that, if set toFALSE, makes these special names only active if prefixed with\DEV\, thus allowing ordinary files to be created with these names.[15]

GEMDOS, the DOS-like part ofAtari TOS, supported similar device names to DOS, but unlike DOS it required a trailing ":" character (on DOS, this is optional) to identify them as devices as opposed to normal filenames (thus "CON:" would work on both DOS and TOS, but "CON" would name an ordinary file on TOS but the console device on DOS). InMiNT andMagiC, a special UNIX-like unified filesystem view accessed via the "U:" drive letter also placed device files in "U:\DEV".

Device keyword[14]Use as inputUse as output
CONReceives typed data until^Z (Ctrl-Z) is pressed.Prints data to the console.
PRN[16]Prints text to the printer, usually redirected toLPT1 orLST. Sometimes reconfigurable to other devices.[17][18][19]
AUX (not in OS/2[16])Reads data from an auxiliary device, usually a serial device likeCOM1. Sometimes reconfigurable to other devices.[17][18][19]Sends data to an auxiliary device, usually a serial device likeCOM1. Sometimes reconfigurable to other devices.[17][18][19]
NULReturns null or no data.Discards received data.
CLOCK$ (still namedCLOCK in some versions of MS-DOS 2.11[20][17][18])
KEYBD$ (only inmultitasking MS-DOS)??
KBD$ (only inOS/2[16])??
SCREEN$ (only in multitasking MS-DOS and OS/2[16])??
POINTER$ (only in OS/2[16])??
MOUSE$ (only in OS/2[16])??
$IDLE$ (only inDR-DOS (since 5.0) andMultiuser DOS (sinceConcurrent DOS 386) families)
CONFIG$ (only in MS-DOS 7.0 and higher)
LST (only in86-DOS and DOS 1.x, also in Hewlett-Packard's MS-DOS 2.11 for theHP Portable Plus[17][18])Returns no data.Sends data to the line printer. (LPT2 for Hewlett-Packard's MS-DOS 2.11[17][18])
PLT (only in Hewlett-Packard's MS-DOS 2.11 for theHP Portable Plus[17][18])Returns no data.Sends data to the assignedplotter. The attached plotter device is reconfigurable.[17][18]
LPT1,LPT2,LPT3, and sometimesLPT4 (in DR-DOS 7.02 and higher and some versions of Multiuser DOS)Sends data to the selected parallel port.
COM1,COM2,COM3,COM4Reads data from the selected serial port.Sends data to the selected serial port.
82164A (only in Hewlett-Packard's MS-DOS 2.11 for theHP Portable Plus[17][18])Redirects to COM2.Redirects to COM2.

Using shellredirection and pipes, data can be sent to or received from a device. For example, typing the following will send the filec:\data.txt to the printer:

TYPE c:\data.txt > PRN

PIPE, MAILSLOT, and MUP are other standard Windows devices.[21]

IOCS

[edit]

The 8-bit operating system ofSharppocket computers like thePC-E500,PC-E500S etc. consists of aBASIC interpreter, a DOS 2-like File Control System (FCS) implementing a rudimentary12-bit FAT-like filesystem, and a BIOS-likeInput/Output Control System (IOCS) implementing a number of standard character and block device drivers as well as special file devices including STDO:/SCRN: (display), STDI:/KYBD: (keyboard), COM: (serial I/O), STDL:/PRN: (printer), CAS: (cassette tape), E:/F:/G: (memory file), S1:/S2:/S3: (memory card), X:/Y: (floppy), SYSTM: (system), and NIL: (function).[22]

Implementations

[edit]
Operating SystemFilesystem or managing softwareStandardmount pointAuthorNotes
Linux 2.3.46pre5–2.6.17devfs[23] anddevfsd/devRichard GoochImplemented fully in the kernel, with optional daemondevfsd to handle device node events in user space.[24] Obsolete – users are encouraged to migrate toudev and/ordevtmpfs.
Linux 2.5–udev on any fs, but usuallytmpfs/devGreg Kroah-Hartman,Kay Sievers andDan StekloffImplemented largely in user space, device information is gathered fromsysfs. Device files can be stored on a conventional general-purpose file system, or in a memory file system (tmpfs).
Linux 2.6.32–devtmpfs with or without udev/devKay Sievers, Jan Blunck,Greg Kroah-HartmanA hybrid kernel/userspace approach of a device filesystem to provide nodes before udev runs for the first time[25]
Solarisdevfs[26]/devicesSun MicrosystemsIntroduced with dynamic loaded drivers in Solaris-2.1
FreeBSD 2.0–devfs/devPoul-Henning KampImplemented fully in the kernel.
DragonFly BSD 2.3.2–devfs/devAlex HornungImplemented fully in the kernel.
macOSdevfs/devApple Inc.Implemented fully in the kernel.
HP-UX B.11.31devfs/devHPImplemented fully in the kernel.
Plan 9#Bell LabsImplemented in the kernel.
RISC OSDeviceFSDevices:Acorn ComputersDeviceFS was started in 1991[27] and first appeared in RISC OS 3. It manages several device like special files, most commonly: Parallel, Serial, FastParallel, and USB. The SystemDevices module implements the pseudo devices such as: Vdu, Kbd, Null and Printer.
MS-DOS,PC DOS,DR-DOSFAT\DEV (and/DEV)variousAs implemented in the kernel, character devices appear in the virtual \DEV directory and any disk directory. Under MS-DOS/PC DOS 2.x, theCONFIG.SYSAVAILDEV=FALSE directive can be used to force devices to exist only in \DEV.
MagiC,MiNT,MultiTOSU:\DEV[28][29]Application Systems Heidelberg, Eric R. Smith,Atari Corp.The special U: drive contains a virtual DEV directory, inside which one can find device files.
Windows 9x\\devices\Microsoft
Windows NT\DeviceMicrosoftThe\Device directory is a part ofWindows NT object namespace.
Windows NT Win32 Subsystem\\.\MicrosoftThe\\.\ prefix makes supporting APIs access the Win32 device namespace instead of the Win32 file namespace. The Win32 device names are symbolic links to device names under Windows NT\Device directory.

See also

[edit]

References

[edit]
  1. ^abMicrosoft MS-DOS Operating System User's Guide(PDF). Microsoft. 1983. p. 3-5. Retrieved2024-04-20.
  2. ^ab"The 3-Letter C Word That Windows Hates".YouTube. 2016-04-14.
  3. ^Kroah-Hartman, Greg (2005-06-20)."[PATCH] devfs: Remove devfs from the kernel tree".Linux kernel source tree. Retrieved2021-06-12.
  4. ^Corbet, Jonathan; Kroah-Hartman, Greg; Rubini, Alessandro (2005). "Access Control on a Device File".Linux Device Drivers, 3rd Edition.O'Reilly. Archived from the original on 2009-09-07. Retrieved2017-04-28.The next step beyond a single-open device is to let a single user open a device in multiple processes but allow only one user to have the device open at a time.
  5. ^Kernighan, Brian W.;Pike, Rob (1984).The UNIX Programming Environment.Prentice-Hall. p. 66.ISBN 0-13-937681-X.
  6. ^Neil Brown (2010-10-27)."Ghosts of Unix Past: a historical search for design patterns".Linux Weekly News. Retrieved2014-03-30.
  7. ^"IEEE Std 1003.1, 2013 Edition". Retrieved2014-04-24.
  8. ^"FreeBSD Architecture Handbook". Retrieved2013-03-07.
  9. ^"usr.sbin/envstat/envstat.c".BSD Cross Reference.NetBSD. November 2021.
  10. ^"mknod(8)".FreeBSD Manual Pages. The FreeBSD Project. 2016-10-03. Retrieved2024-04-21.
  11. ^Linux Assigned Names and Numbers Authority (2009-04-06)."Linux allocated devices (2.6+ version)".Linux kernel (Documentation/devices.txt). Archived fromthe original on 2016-04-24. Retrieved2013-06-08.
  12. ^"Avoid Creating Macintosh Filenames that are NT Device Names". Support.microsoft.com. 2006-11-01. Retrieved2014-01-22.
  13. ^"device attributes". Stanislavs.org. Retrieved2014-01-22.
  14. ^ab"MS-DOS Device Driver Names Cannot be Used As File Names". Revision 2.0.Microsoft. 2003-05-12. KB74496, Q74496. Archived fromthe original on 2012-07-21.
  15. ^"Undocumented Commands".4dos.info. Kevtronics. 2002-04-12. Retrieved2014-05-16.
  16. ^abcdefIBM Operating System/2 Technical Reference - Programming Family(PDF). Vol. 1 (1st ed.).IBM. September 1987 [1986].
  17. ^abcdefghiHewlett-Packard - Technical Reference Manual - Portable PLUS (1 ed.). Corvallis, OR, USA:Hewlett-Packard Company, Portable Computer Division. August 1985. 45559-90001. Retrieved2016-11-27.
  18. ^abcdefghiHewlett-Packard - Technical Reference Manual - Portable PLUS(PDF) (2 ed.). Portable Computer Division, Corvallis, OR, USA:Hewlett-Packard Company. December 1986 [August 1985]. 45559-90006.Archived(PDF) from the original on 2016-11-28. Retrieved2016-11-27.
  19. ^abcPaul, Matthias R. (1997-10-02)."Caldera OpenDOS 7.01/7.02 Update Alpha 3 IBMBIO.COM README.TXT". Archived fromthe original on 2003-10-04. Retrieved2009-03-29.[1]
  20. ^Paterson, Tim; Microsoft (2013-12-19) [1983]."Microsoft DOS V1.1 and V2.0: /msdos/v20source/SKELIO.TXT, /msdos/v20source/HRDDRV.ASM".Computer History Museum,Microsoft. Retrieved2014-03-25. (Note: While the publishers claim this would be MS-DOS 1.1 and 2.0, it actually isSCP MS-DOS 1.25 and a mixture ofAltos MS-DOS 2.11 andTeleVideo PC DOS 2.11.)
  21. ^"REG: CurrentControlSet Entries PART 2: SessionManager". Support.microsoft.com. 2006-11-01. Retrieved2014-01-22.
  22. ^Technical Reference Manual PC-E500(PDF).Sharp Corporation, Information Systems Group, Personal Equipment Division. March 1990. p. 17. Archived fromthe original(PDF) on 2017-03-14. Retrieved2017-03-14.
  23. ^Gooch, Richard (2002-08-20)."Linux Devfs (Device File System) FAQ". Retrieved2021-06-13.
  24. ^Gooch, Richard."My Linux Contributions". Retrieved2021-06-13.Devfsd provides configurable management of device nodes using the Linux Device Filesystem.
  25. ^"Driver Core: devtmpfs - kernel-maintained tmpfs-based /dev". LWN. Retrieved2009-08-10.
  26. ^"devfs(7FS)".man pages section 7: Device and Network Interfaces. Oracle. 2014. Retrieved2021-06-12.
  27. ^"Project Black change log". Retrieved2016-05-15.
  28. ^"The drive U: in MagiC". 2016-03-28.Archived from the original on 2017-01-15. Retrieved2017-01-09.
  29. ^"FreeMiNT-Portal - mint.doc". 2000-04-27.Archived from the original on 2017-01-15. Retrieved2017-01-09.

Further reading

[edit]
Disk and
non-rotating
Optical disc
Flash memory andSSD
host-sidewear leveling
Distributed parallel
NAS
Specialized
Pseudo
Encrypted
Types
Features
Access control
Interfaces
Lists
Layouts
MS-DOS, IBM PC DOS,
compatible systems
Otherx86
Other platforms
Retrieved from "https://en.wikipedia.org/w/index.php?title=Device_file&oldid=1278565680#BLOCKDEV"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp