xref: /third_party/toybox/www/doc/mount.txt (revision 0f66f451)
10f66f451Sopenharmony_ciHere's how mount actually works:
20f66f451Sopenharmony_ci
30f66f451Sopenharmony_ciThe mount comand calls the mount system call, which has five arguments you
40f66f451Sopenharmony_cican see on the "man 2 mount" page:
50f66f451Sopenharmony_ci
60f66f451Sopenharmony_ci  int mount(const char *source, const char *target, const char *filesystemtype,
70f66f451Sopenharmony_ci            unsigned long mountflags, const void *data);
80f66f451Sopenharmony_ci
90f66f451Sopenharmony_ciThe command "mount -t ext2 /dev/sda1 /path/to/mntpoint -o ro,noatime",
100f66f451Sopenharmony_ciparses its command line arguments to feed them into those five system call
110f66f451Sopenharmony_ciarguments. In this example, the source is "/dev/sda1", the target is
120f66f451Sopenharmony_ci"/path/to/mountpoint", and the filesystemtype is "ext2".
130f66f451Sopenharmony_ci
140f66f451Sopenharmony_ciThe other two syscall arguments (mountflags and data) come from the
150f66f451Sopenharmony_ci"-o option,option,option" argument. The mountflags argument goes to the VFS
160f66f451Sopenharmony_ci(explained below), and the data argument is passed to the filesystem driver.
170f66f451Sopenharmony_ci
180f66f451Sopenharmony_ciThe mount command's options string is a list of comma separated values. If
190f66f451Sopenharmony_cithere's more than one -o argument on the mount command line, they get glued
200f66f451Sopenharmony_citogether (in order) with a comma. The mount command also checks the file
210f66f451Sopenharmony_ci/etc/fstab for default options, and the options you specify on the command
220f66f451Sopenharmony_ciline get appended to those defaults (if any). Most other command line mount
230f66f451Sopenharmony_ciflags are just synonyms for adding option flags (for example
240f66f451Sopenharmony_ci"mount -o remount -w" is equivalent to "mount -o remount,rw"). Behind the
250f66f451Sopenharmony_ciscenes they all get appended to the -o string and fed to a common parser.
260f66f451Sopenharmony_ci
270f66f451Sopenharmony_ciVFS stands for "Virtual File System" and is the common infrastructure shared
280f66f451Sopenharmony_ciby different filesystems. It handles common things like making the filesystem
290f66f451Sopenharmony_ciread only. The mount command assembles an option string to supply to the "data"
300f66f451Sopenharmony_ciargument of the option syscall, but first it parses it for VFS options
310f66f451Sopenharmony_ci(ro,noexec,nodev,nosuid,noatime...) each of which corresponds to a flag
320f66f451Sopenharmony_cifrom #include <sys/mount.h>. The mount command removes those options from the
330f66f451Sopenharmony_cisting and sets the corresponding bit in mountflags, then the remaining options
340f66f451Sopenharmony_ci(if any) form the data argument for the filesystem driver.
350f66f451Sopenharmony_ci
360f66f451Sopenharmony_ciA few quick implementation details: the mountflag MS_SILENCE gets set by
370f66f451Sopenharmony_cidefault even if there's nothing in /etc/fstab. Some actions (such as --bind
380f66f451Sopenharmony_ciand --move mounts, I.E. -o bind and -o move) are just VFS actions and don't
390f66f451Sopenharmony_cirequire any specific filesystem at all. The "-o remount" flag requires looking
400f66f451Sopenharmony_ciup the filesystem in /proc/mounts and reassembling the full option string
410f66f451Sopenharmony_cibecause you don't _just_ pass in the changed flags but have to reassemble
420f66f451Sopenharmony_cithe complete new filesystem state to give the system call. Some of the options
430f66f451Sopenharmony_ciin /etc/fstab are for the mount command (such as "user" which only does
440f66f451Sopenharmony_cianything if the mount command has the suid bit set) and don't get passed
450f66f451Sopenharmony_cithrough to the system call.
460f66f451Sopenharmony_ci
470f66f451Sopenharmony_ciWhen mounting a new filesystem, the "filesystem" argument to the mount system
480f66f451Sopenharmony_cicall specifies which filesystem driver to use. All the loaded drivers are
490f66f451Sopenharmony_cilisted in /proc/filesystems, but calling mount can also trigger a module load
500f66f451Sopenharmony_cirequest to add another. A filesystem driver is responsible for putting files
510f66f451Sopenharmony_ciand subdirectories under the mount point: any time you open, close, read,
520f66f451Sopenharmony_ciwrite, truncate, list the contents of a directory, move, or delete a file,
530f66f451Sopenharmony_ciyou're talking to a filesystem driver to do it. (Or when you call
540f66f451Sopenharmony_ciioctl(), stat(), statvfs(), utime()...)
550f66f451Sopenharmony_ci
560f66f451Sopenharmony_ciDifferent drivers implement different filesystems, which have four categories:
570f66f451Sopenharmony_ci
580f66f451Sopenharmony_ci1) Block device backed filesystems, such as ext2 and vfat.
590f66f451Sopenharmony_ci
600f66f451Sopenharmony_ciThis kind of filesystem driver acts as a lens to look at a block device
610f66f451Sopenharmony_cithrough. The source argument for block backed filesystems is a path to a
620f66f451Sopenharmony_ciblock device, such as "/dev/hda1", which stores the contents of the
630f66f451Sopenharmony_cifilesystem in a fixed block of sequential storage, and there's a seperate
640f66f451Sopenharmony_cidriver providing that block device.
650f66f451Sopenharmony_ci
660f66f451Sopenharmony_ciBlock backed filesystems are the "conventional" filesystem type most people
670f66f451Sopenharmony_cithink of when they mount things. The name means that the "backing store"
680f66f451Sopenharmony_ci(where the data lives when the system is switched off) is on a block device.
690f66f451Sopenharmony_ci
700f66f451Sopenharmony_ci2) Server backed filesystems, such as cifs/samba or fuse.
710f66f451Sopenharmony_ci
720f66f451Sopenharmony_ciThese drivers convert filesystem operations into a sequential stream of
730f66f451Sopenharmony_cibytes, which it can send through a pipe to talk to a program. The filesystem
740f66f451Sopenharmony_ciserver could be a local Filesystem in Userspace daemon (connected to a local
750f66f451Sopenharmony_ciprocess through a pipe filehandle), behind a network socket (CIFS and v9fs),
760f66f451Sopenharmony_cibehind a char device (/dev/ttyS0), and so on. The common attribute is there's
770f66f451Sopenharmony_cisome program on the other end sending and receiving a sequential bytestream.
780f66f451Sopenharmony_ciThe backing store is a server somewhere, and the filesystem driver is talking
790f66f451Sopenharmony_cito a process that reads and writes data in some known protocol.
800f66f451Sopenharmony_ci
810f66f451Sopenharmony_ciThe source argument for these filesystems indicates where the filesystem lives. It's often in a URL-like format for network filesystems, but it's really just a blob of data that the filesystem driver understands.
820f66f451Sopenharmony_ci
830f66f451Sopenharmony_ciA lot of server backed filesystems want to open their own connection so they
840f66f451Sopenharmony_cidon't have to pass their data through a persistent local userspace process,
850f66f451Sopenharmony_cinot really for performance reasons but because in low memory situations a
860f66f451Sopenharmony_cichicken-and-egg situation can develop where all the process's pages have
870f66f451Sopenharmony_cibeen swapped out but the filesystem needs to write data to its backing
880f66f451Sopenharmony_cistore in order to free up memory so it can swap the process's pages back in.
890f66f451Sopenharmony_ciIf this mechanism is providing the root filesystem, this can deadlock and
900f66f451Sopenharmony_cifreeze the system solid. So while you _can_ pass some of them a filehandle,
910f66f451Sopenharmony_cimore often than not you don't.
920f66f451Sopenharmony_ci
930f66f451Sopenharmony_ciThese are also known as "pipe backed" filesystems (or "network filesystems"
940f66f451Sopenharmony_cibecause that's a common case, although a network doesn't need to be inolved).
950f66f451Sopenharmony_ciConceptually they're char device backed filesystems (analogus to the block
960f66f451Sopenharmony_cidevice backed ones), but you don't commonly specify a character device in
970f66f451Sopenharmony_ci/dev when mounting them because you're talking to a specific server process,
980f66f451Sopenharmony_cinot a whole machine.
990f66f451Sopenharmony_ci
1000f66f451Sopenharmony_ci3) Ram backed filesystems, such as ramfs and tmpfs.
1010f66f451Sopenharmony_ci
1020f66f451Sopenharmony_ciThese are very simple filesystems that don't implement a backing store. Data
1030f66f451Sopenharmony_ciwritten to these gets stored in the disk cache, and the driver ignores requests
1040f66f451Sopenharmony_cito flush it to backing store (reporting all the pages as pinned and
1050f66f451Sopenharmony_ciunfreeable).
1060f66f451Sopenharmony_ci
1070f66f451Sopenharmony_ciThese drivers essentially mount the VFS's page/dentry cache as if it was a
1080f66f451Sopenharmony_cifilesystem. (Page cache stores file contents, dentry cache stores directory
1090f66f451Sopenharmony_cientries.) They grow and shrink dynamically, as needed: when you write files
1100f66f451Sopenharmony_ciinto them they allocate more memory to store it, and when you delete files
1110f66f451Sopenharmony_cithe memory is freed.
1120f66f451Sopenharmony_ci
1130f66f451Sopenharmony_ciThere's a simple one (ramfs) that does only that, and a more complex one (tmpfs)
1140f66f451Sopenharmony_ciwhich adds a size limitation (by default 50%, but it's adjustable as a mount
1150f66f451Sopenharmony_cioption) so the system doesn't run out of memory and lock up if you
1160f66f451Sopenharmony_ci"cat /dev/zero > file", and can also report how much space is remaining
1170f66f451Sopenharmony_ciwhen asked (ramfs always says 0 bytes free). The other thing tmpfs does
1180f66f451Sopenharmony_ciis write its data out to swap space (like processes do) when the system
1190f66f451Sopenharmony_ciis under memory proessure.
1200f66f451Sopenharmony_ci
1210f66f451Sopenharmony_ciNote that "ramdisk" is not the same as "ramfs". The ramdisk driver uses a
1220f66f451Sopenharmony_cichunk of memory to implement a block device, and then you can format that
1230f66f451Sopenharmony_ciblock device and mount it with a block device backed filesystem driver.
1240f66f451Sopenharmony_ci(This is the same "two device drivers" approach you always have with block
1250f66f451Sopenharmony_cibacked filesystems: one driver provides /dev/ram0 and the second driver mounts
1260f66f451Sopenharmony_ciit as vfat.) Ram disks are significantly less efficient than ramfs,
1270f66f451Sopenharmony_ciallocating a fixed amount of memory up front for the block device instead of
1280f66f451Sopenharmony_cidynamically resizing itself as files are written into an deleted from the
1290f66f451Sopenharmony_cipage and dentry caches the way ramfs does.
1300f66f451Sopenharmony_ci
1310f66f451Sopenharmony_ciNote: initramfs cpio, tmpfs as rootfs.
1320f66f451Sopenharmony_ci
1330f66f451Sopenharmony_ci4) Synthetic filesystems, such as proc, sysfs, devpts...
1340f66f451Sopenharmony_ci
1350f66f451Sopenharmony_ciThese filesystems don't have any backing store either, because they don't
1360f66f451Sopenharmony_cistore arbitrary data the way the first three types of filesystems do.
1370f66f451Sopenharmony_ci
1380f66f451Sopenharmony_ciInstead they present artificial contents, which can represent processes or
1390f66f451Sopenharmony_cihardware or anything the driver writer wants them to show. Listing or reading
1400f66f451Sopenharmony_cifrom these files calls a driver function that produces whatever output it's
1410f66f451Sopenharmony_ciprogrammed to, and writing to these files submits data to the driver which
1420f66f451Sopenharmony_cican do anything it wants with it.
1430f66f451Sopenharmony_ci
1440f66f451Sopenharmony_ciSynthetic ilesystems are often implemented to provide monitoring and control
1450f66f451Sopenharmony_ciknobs for parts of the operating system. It's an alternative to adding more
1460f66f451Sopenharmony_cisystem calls (or ioctl, sysctl, etc), and provides a more human friendly user
1470f66f451Sopenharmony_ciinterface which programs can use but which users can also interact with
1480f66f451Sopenharmony_cidirectly from the command line via "cat" and redirecting the output of
1490f66f451Sopenharmony_ci"echo" into special files.
1500f66f451Sopenharmony_ci
1510f66f451Sopenharmony_ci
1520f66f451Sopenharmony_ciThose are the four types of filesystems: backing store can be a fixed length
1530f66f451Sopenharmony_ciblock of storage, backing store can be some server the driver connects to,
1540f66f451Sopenharmony_cibacking store can not exist and the files merely reside in the disk cache,
1550f66f451Sopenharmony_cior the filesystem driver can just make up its contents programmatically.
1560f66f451Sopenharmony_ci
1570f66f451Sopenharmony_ciAnd that's how filesystems get mounted, using the mount system call which has
1580f66f451Sopenharmony_cifive arguments. The "filesystem" argument specifies the driver implementing
1590f66f451Sopenharmony_cione of those filesystems, and the "source" and "data" arguments get fed to
1600f66f451Sopenharmony_cithat driver. The "target" and "mountflags" arguments get parsed (and handled)
1610f66f451Sopenharmony_ciby the generic VFS infrastructure. (The filesystem driver can peek at the
1620f66f451Sopenharmony_ciVFS data, but generally doesn't need to care. The VFS tells the filesystem
1630f66f451Sopenharmony_ciwhat to do, in response to what userspace said to do.)
164