FreeBSD src
Go to file
Xin LI b06cf1e44f Reduce memory footprint of fsck_msdosfs.
This utility was initially written for FAT12/16, which were inherently
small. When FAT32 support was added, the old data structure and
algorithms remain used with minimal changes.

With growing size of FAT32 media, the current data structure that
requires 4 32-bit variables per each FAT32 table entry would consume up
to 4 GiB of RAM, which can be too big for systems with limited RAM
available.

Address this by taking a different approach of validating the FAT.

The FAT is essentially a set of linked lists of chains that was
referenced by directory entries, and the checker needs to make sure that
the linked chains of clusters do not have cross-linked chains, and every
chain were referenced by one and only one directory entry.  Instead of
keeping track of the chain's 'head' cluster number, the size of the
chain, the used status of the chain and the "next" pointer which is
content of the FAT table, we create accessors for the FAT table data
for the "next" pointer, and keep only one bit to indicate if the
current cluster is a 'head' node of a cluster chain, in a bitmap.

We further overhaul the FAT checker to find out the possible head nodes
by excluding ones that are not (in other words, nodes that have some
other nodes claiming them as the next node) instead of marking the head
nodes for each node on the chain.  This approach greatly reduced the
complexiety of computation from O(N^2) worst case, to an O(N) scan for
worst case.  The file (cluster chain) length is not useful for the FAT
checker, so don't bother to calculate them in the FAT checker and
instead leave the task to the directory structure check, at which point
we would have non-crossed cluster chains, and we are guaranteed that
each cluster will be visited for at most one time.

When checking the directory structures, we use the head node indicator
to as the visited (used) flag: every cluster chain can only be
referenced by one directory entry, so we clear them when calculating
the length of the chain, and we can immediately tell if there are
anomalies in the directory entry.

As a result, the required RAM size is now 1 bit per each entry of
the FAT table, plus memory needed to hold the FAT table in memory,
instead of 16 bytes (=128 bits) per each entry.  For FAT12 and FAT16,
we will load the whole FAT table into memory as they are smaller than
128KiB, and for FAT32, we first attempt to mmap() it into memory, and
when that fails, we would fall back to a simple LRU cache of 4 MiB of
RAM.

sbin/fsck_msdosfs/boot.c:

 - Added additional sanity checks for valid FAT32/FAT16/FAT12 cluster
   number.
 - FAT32: check if root directory starts with a valid cluster number,
   moved from dir.c.  There is no point to proceed if the filesystem
   is already damaged beyond repair.

sbin/fsck_msdosfs/check.c:

 - Combine phase 1 and phase 2, now that the readfat() is able to
   detect cross chains.

sbin/fsck_msdosfs/dir.c:

 - Refactor code to use FAT accessor instead of accessing the internal
   representation of FAT table.
 - Make use of the cluster chain head bitmap.
 - Clarify and simplify directory entry check, remove unnecessary
   checks that are would be done at a later time (for example, whether
   the directory's second cluster is a valid one, which is examined
   more throughly in a later checkchain() and does not prevent us
   from proceeding further).

sbin/fsck_msdosfs/dosfs.h:

 - Remove internal representation of FAT table, which is replaced by
   the head bitmap that is opaque to other code.
 - Added a special CLUST_DEAD cluster type to indicate errors.

sbin/fsck_msdosfs/ext.h:

 - Added a flag that overrides mmap(2) setting.  The corresponding
   command line option, -M is intentionally undocumented as we do not
   expect users to need it.
 - Added accessors for FAT table and convert existing interface to use
   it.

sbin/fsck_msdosfs/fat.c:

 - Added head bitmap to represent whether a cluster is a head cluster.
 - Converted FAT internal representation to accessors.
 - Implemented a LRU cache for FAT32 when mmap(2) should not or can not
   be used.
 - _readfat: Attempt a mmap(2) and fall back to regular read for
   non-FAT32 file systems; use the LRU cache for FAT32 and prepopulate
   the cache with the first 4MiB of the entries.
 - readfat: Added support of head bitmap and use the population scan to
   detect bogus chains.
 - clusterdiff: removed, FATs are copied from the checked copy via
   writefat()/copyfat().
 - checkchain: calculates the length of a cluster chain and make sure
   that it ends with a valid EOF marker.
 - clearchain: follow and clear a chain and maintain the free cluster
   count.
 - checklost: convert to use head bitmap. At the end of all other scans,
   the remaining 'head' nodes are leaders of lost cluster chains.

sbin/fsck_msdosfs/fat.c:

 - Added a new -M option which is intentionally undocumented, to disable
   the use of mmap().

Reviewed by:	kevlo
MFC after:	1 month
Relnotes:	yes
Differential Revision:	https://reviews.freebsd.org/D22965
2020-01-01 07:43:08 +00:00
bin sh: Test that executing various binary files is rejected 2019-12-30 21:32:55 +00:00
cddl Add libdtrace support for arm64 USDT probes. 2019-12-29 21:46:50 +00:00
contrib MFV r356143: 2019-12-31 15:50:41 +00:00
crypto sshd: make getpwclass wrapper MON_ISAUTH not MON_AUTH 2019-11-20 16:30:37 +00:00
etc Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp 2019-12-22 11:50:44 +00:00
gnu Move all sources from the llvm project into contrib/llvm-project. 2019-12-20 19:53:05 +00:00
include Revert r355760, r355759 2019-12-15 17:33:26 +00:00
kerberos5 Update Makefile.depend files 2019-12-11 17:37:53 +00:00
lib MFV r356163,r356197: 2019-12-31 00:05:06 +00:00
libexec rtld(1): Do booleans like C99 2019-12-31 05:41:47 +00:00
release release: chase ports r519089 (rpi-firmware update) 2019-12-05 16:17:56 +00:00
rescue Remove unused defines since r147075 2019-07-12 04:44:50 +00:00
sbin Reduce memory footprint of fsck_msdosfs. 2020-01-01 07:43:08 +00:00
secure Update Makefile.depend files 2019-12-11 17:37:53 +00:00
share Formatting fixes for tables, no content changes. 2019-12-30 22:05:57 +00:00
stand [PowerPC64] Use ld.bfd to build LIB32 and STAND - when using llvm 2019-12-24 16:03:33 +00:00
sys vmgenid(4): Integrate as a random(4) source 2020-01-01 00:35:02 +00:00
targets Update dirdeps.mk and gendirdeps.mk 2019-12-11 17:38:15 +00:00
tests Add userland tests for route table/lltable rtsock operations. 2019-12-28 12:16:40 +00:00
tools Change reference in HTTPD descriptions to 'simple_httpd' 2019-12-29 02:11:58 +00:00
usr.bin Update the man page to reflect that "-M" and "-N" are deprecated and ignored. 2019-12-31 22:25:06 +00:00
usr.sbin inetd: convert remaining bzero(3) to memset(3), NFC 2020-01-01 04:29:08 +00:00
.arcconfig callsign isn't required anymore 2016-09-29 06:19:45 +00:00
.arclint arc lint: ignore /tests/ in chmod 2017-12-19 03:38:06 +00:00
.cirrus.yml Update vendor/libarchive/dist to git 1dae5a549fe4ab99fd3a49a9edcf897a7b2b1844 2019-12-28 23:40:32 +00:00
.clang-format Add a basic clang-format configuration file 2019-06-07 15:23:52 +00:00
.gitattributes Add a basic clang-format configuration file 2019-06-07 15:23:52 +00:00
.gitignore Vendor import of Unbound 1.9.6. 2019-12-28 05:27:06 +00:00
COPYRIGHT Happy New Year 2020! 2019-12-31 16:01:36 +00:00
LOCKS LOCKS: update current locks 2018-06-09 03:08:04 +00:00
MAINTAINERS Move all sources from the llvm project into contrib/llvm-project. 2019-12-20 19:53:05 +00:00
Makefile Take arm.arm (armv5) out of universe 2019-11-03 19:36:34 +00:00
Makefile.inc1 revert r355609 2019-12-13 14:48:44 +00:00
Makefile.libcompat libcompat: build 32-bit rtld and ldd as part of "everything" 2019-11-07 22:58:10 +00:00
Makefile.sys.inc AUTO_OBJ: For all top-level targets enforce using an OBJDIR. 2017-12-05 21:29:47 +00:00
ObsoleteFiles.inc Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp 2019-12-22 11:50:44 +00:00
README README: add generic notes about GENERIC and NOTES 2018-06-17 19:44:24 +00:00
README.md Vendor import of Unbound 1.9.6. 2019-12-28 05:27:06 +00:00
RELNOTES Add an entry to RELNOTES for r355677. 2019-12-13 16:28:48 +00:00
UPDATING Add a note to UPDATING on the move to clang for powerpc* 2019-12-27 04:42:03 +00:00

FreeBSD Source:

This is the top level of the FreeBSD source directory. This file was last revised on: FreeBSD

FreeBSD is an operating system used to power modern servers, desktops, and embedded platforms. A large community has continually developed it for more than thirty years. Its advanced networking, security, and storage features have made FreeBSD the platform of choice for many of the busiest web sites and most pervasive embedded networking and storage devices.

For copyright information, please see the file COPYRIGHT in this directory. Additional copyright information also exists for some sources in this tree - please see the specific source directories for more information.

The Makefile in this directory supports a number of targets for building components (or all) of the FreeBSD source tree. See build(7), config(8), https://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/makeworld.html, and https://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/kernelconfig.html for more information, including setting make(1) variables.

Source Roadmap:

bin		System/user commands.

cddl		Various commands and libraries under the Common Development
		and Distribution License.

contrib		Packages contributed by 3rd parties.

crypto		Cryptography stuff (see crypto/README).

etc		Template files for /etc.

gnu		Various commands and libraries under the GNU Public License.
		Please see gnu/COPYING* for more information.

include		System include files.

kerberos5	Kerberos5 (Heimdal) package.

lib		System libraries.

libexec		System daemons.

release		Release building Makefile & associated tools.

rescue		Build system for statically linked /rescue utilities.

sbin		System commands.

secure		Cryptographic libraries and commands.

share		Shared resources.

stand		Boot loader sources.

sys		Kernel sources.

sys/<arch>/conf Kernel configuration files. GENERIC is the configuration
		used in release builds. NOTES contains documentation of
		all possible entries.

tests		Regression tests which can be run by Kyua.  See tests/README
		for additional information.

tools		Utilities for regression testing and miscellaneous tasks.

usr.bin		User commands.

usr.sbin	System administration commands.

For information on synchronizing your source tree with one or more of the FreeBSD Project's development branches, please see:

https://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/current-stable.html