7 of 18 added 1725 characters in body

Luciano Andress Martini pointed out this lets you run umount / and then fsck /dev/hda2. (Assuming there are no files open for writing, e.g. because the system is running in single-user mode). This means if you don't know how to remount a filesystem as read-only, you can desperately try the same commands as if you needed to fsck your /home filesystem or /dev/fd0, and it can work... even though fsck is on the filesystem you apparently unmounted :-). It's nice that Linux can be helpful like this, when you are trying to recover a corrupted system.

The other known use of this special case is in old shutdown scripts, which ran umount -a. This being defined to simply unmount all filesystems in reverse order, finishing with the root filesystem.

I'm not certain why this is implemented in the kernel, rather than in the umount command. One reason might be that old kernels accepted the name of the mounted device, instead of the directory the filesystem was mounted at. So perhaps putting the check in the kernel was considered simpler or more reliable.

The same code comment is present in early versions of Linux including 0.99.10 (1993). On the other hand it does not seem to be a standard for traditional UNIX. The FreeBSD kernel returns an error instead, and the FreeBSD equivalent of umount -a stops before unmounting the first filesystem i.e. the root. (The code is here, but you need to understand how for loops and array indexes work in C :-).

The special case is not documented in the current man pages for umount(2) or umount(8). So the current man page implies that umount -a will always fail, but this is not the case. You might guess that umount -a is not very widely used nowadays.

The old scripts contrast with current scripts for SysVinit, for example in Debian. /etc/init.d/umount_root explicitly remounts / as readonly. The rest of the mounts are processed individually, by /etc/init.d/umountfs and /etc/init.d/umountnfs.sh.

umount -a is not ideal on modern systems. E.g. it is simpler to leave a /proc filesystem mounted so that /proc/mounts can still be used. And /dev is usually a separate mounted filesystem, which will show an unmount error because /dev/console is still open.

For an example of an old shutdown script, see the reference script etc/rc.d/rc.0 in the old SysVinit-2.4.tar.z / SysVinit-2.4.tar.gz.

#! /bin/sh
# brc       This file is executed by init(8) when the system is being
#       shutdown (i.e. set to run at level 0).  It usually takes
#       care of un-mounting al unneeded file systems.
# Version:  @(#)/etc/brc        2.01    02/17/93
# Authors:  Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
#       Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>

  echo Unmounting file systems.....
  umount -a
  echo Done.