freebsd-dev/sbin
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
..
adjkerntz various: general adoption of SPDX licensing ID tags. 2017-11-27 15:37:16 +00:00
bectl Update Makefile.depend files 2019-12-11 17:37:53 +00:00
bsdlabel Remove deprecated GEOM classes 2019-08-13 20:06:55 +00:00
camcontrol Declare packed struct ata_params as 2-byte-aligned 2019-12-21 02:44:00 +00:00
ccdconfig ccdconfig: Move VCS tags to be more consistent with our style. 2017-12-30 00:26:42 +00:00
clri In preparation for adding inode check-hashes, clean up and 2018-11-13 21:40:56 +00:00
comcontrol Remove sio(4). 2019-11-21 01:24:49 +00:00
conscontrol Remove sio(4). 2019-11-21 01:24:49 +00:00
ddb ddb: use 'textdump dump' instead of 'call doadump' 2019-10-18 12:32:01 +00:00
decryptcore EKCD: Add Chacha20 encryption mode 2019-05-23 20:12:24 +00:00
devd Add missing "ereport." prefixes of ZFS events. 2019-12-18 18:43:44 +00:00
devfs Move all devfs related files to sbin/devfs/ 2018-08-22 15:55:23 +00:00
devmatch Update Makefile.depend files 2019-12-11 17:37:53 +00:00
dhclient Update Makefile.depend files 2019-12-11 17:37:53 +00:00
dmesg General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
dump Clarify that the -r and -R options can be used only to create level 0 dumps. 2019-06-14 06:10:15 +00:00
dumpfs The goal of this change is to prevent accidental foot shooting by 2018-02-08 23:06:58 +00:00
dumpon Update Makefile.depend files 2019-12-11 17:37:53 +00:00
etherswitchcfg Add the 'drop tagged' flag support for ethernet switch ports. 2019-06-28 22:12:43 +00:00
fdisk Remove deprecated GEOM classes 2019-08-13 20:06:55 +00:00
ffsinfo In preparation for adding inode check-hashes, clean up and 2018-11-13 21:40:56 +00:00
fsck Remove unused includes. 2019-12-22 05:44:29 +00:00
fsck_ffs When running fsck_ffs manually, do not ask: 2019-12-24 23:03:12 +00:00
fsck_msdosfs Reduce memory footprint of fsck_msdosfs. 2020-01-01 07:43:08 +00:00
fsdb After a crash, a file that extends into indirect blocks may end up 2019-02-25 21:58:19 +00:00
fsirand fsirand(8): Just use arc4random(3) 2019-12-13 04:12:13 +00:00
gbde gbde(8) - simplify randomisation with arc4random_buf 2019-02-11 00:11:02 +00:00
geom Remove all the RELEASE_CRUNCH defines, they are useless. 2019-12-16 21:06:24 +00:00
ggate Fix typo not -> nor and add 'the' because we are talking about specific flags 2019-09-06 15:01:47 +00:00
growfs Normally when an attempt is made to mount a UFS/FFS filesystem whose 2018-12-06 00:09:39 +00:00
gvinum gvinum: revert WARNS change in Makefile 2018-06-17 01:39:22 +00:00
hastctl various: general adoption of SPDX licensing ID tags. 2017-11-27 15:37:16 +00:00
hastd Fix typo in hastd.8 manual page. 2019-12-22 01:22:51 +00:00
ifconfig lagg: Clean up handling of the rr_limit option. 2019-12-22 21:56:47 +00:00
init add ability to set watchdog timeout for a shutdown 2019-10-03 11:23:10 +00:00
ipf Create a link to the ipmon.conf.5 man page as documented in ipmon.5. 2019-06-27 12:37:44 +00:00
ipfw Use strlcat(). 2019-11-30 05:57:54 +00:00
iscontrol Fix formatting. 2019-08-08 20:56:52 +00:00
kldconfig various: general adoption of SPDX licensing ID tags. 2017-11-27 15:37:16 +00:00
kldload various: general adoption of SPDX licensing ID tags. 2017-11-27 15:37:16 +00:00
kldstat Allow three digits of module id without breaking table alignment. 2018-07-02 09:14:00 +00:00
kldunload various: general adoption of SPDX licensing ID tags. 2017-11-27 15:37:16 +00:00
ldconfig Make ldconfig(8) atomic, by removing an unneccessary call to unlink(2) 2018-08-09 11:46:12 +00:00
md5 Close filedescriptors when done with them. 2019-04-19 06:49:46 +00:00
mdconfig Use VOP_ADVISE() with POSIX_FADV_DONTNEED instead of IO_DIRECT to 2018-12-21 08:15:31 +00:00
mdmfs mdmfs(8): add -k skel option to populate fs from a skeleton 2019-11-01 03:10:53 +00:00
mknod General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
mksnap_ffs various: general adoption of SPDX licensing ID tags. 2017-11-27 15:37:16 +00:00
mount Add two options to allow mount to avoid covering up existing mount points. 2019-09-23 04:28:07 +00:00
mount_cd9660 Advise reader to also see mdconfig(8) in mount_cd9660(8). 2018-08-11 08:34:24 +00:00
mount_fusefs Correct a handful of typos/grammos. 2019-12-07 15:17:00 +00:00
mount_msdosfs mount_msdosfs: do not fail mounts requiring locale name conversion table 2018-10-27 16:41:34 +00:00
mount_nfs Fix mount_nfs to recognize the NFSv4 specific errors returned by nmount(2). 2019-12-26 22:33:20 +00:00
mount_nullfs General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
mount_udf General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
mount_unionfs General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
natd DIRDEPS_BUILD: Update dependencies. 2017-10-31 00:07:04 +00:00
newfs Correct the location of the first backup superblock in fsck_ffs.8. 2019-08-07 16:56:00 +00:00
newfs_msdos Fix a couple of minor issues with newfs_msdos: 2019-12-08 01:20:37 +00:00
nfsiod pkgbase: Create a FreeBSD-utilities package and make it the default one 2019-09-05 14:15:47 +00:00
nos-tun pkgbase: Create a FreeBSD-utilities package and make it the default one 2019-09-05 14:15:47 +00:00
nvmecontrol Size is unsigned, so remove the test entirely. 2019-09-25 07:51:30 +00:00
pfctl pfctl: Warn users when they run into kernel limits 2019-12-14 02:03:47 +00:00
pfilctl Hopefully fix compilation by other compilers. 2019-02-01 00:34:18 +00:00
pflogd DIRDEPS_BUILD: Update dependencies. 2017-10-31 00:07:04 +00:00
ping Update Makefile.depend files 2019-12-11 17:37:53 +00:00
ping6 Update Makefile.depend files 2019-12-11 17:37:53 +00:00
quotacheck Normally when an attempt is made to mount a UFS/FFS filesystem whose 2018-12-06 00:09:39 +00:00
rcorder pkgbase: Move rc scripts and related files to their own packages 2019-09-05 14:08:45 +00:00
reboot Remove sio(4). 2019-11-21 01:24:49 +00:00
recoverdisk recoverdisk(1): fclose() file supplied via '-r readlist' parameter when 2019-02-07 14:40:28 +00:00
resolvconf sbin: normalize paths using SRCTOP-relative paths or :H when possible 2017-03-04 11:33:01 +00:00
restore Re-enable reading byte swapped NFS_MAGIC dumps. 2018-08-11 16:12:23 +00:00
route route(8): clarify -prefixlen description 2019-01-10 00:10:12 +00:00
routed When bind fails, make sure we closed the socket we tried to bind the 2017-12-28 05:34:24 +00:00
rtsol Update Makefile.depend files 2019-12-11 17:37:53 +00:00
savecore Update Makefile.depend files 2019-12-11 17:37:53 +00:00
sconfig DIRDEPS_BUILD: Update dependencies. 2017-10-31 00:07:04 +00:00
setkey pkgbase: Create a FreeBSD-utilities package and make it the default one 2019-09-05 14:15:47 +00:00
shutdown shutdown: Fix r327476 by adding init 2018-01-02 09:02:42 +00:00
spppcontrol various: general adoption of SPDX licensing ID tags. 2017-11-27 15:37:16 +00:00
sunlabel Remove deprecated GEOM classes 2019-08-13 20:06:55 +00:00
swapon Fix a logic bug in error handling code. It is an error if p == NULL. 2019-12-09 07:18:40 +00:00
sysctl evdev: export event device properties through sysctl interface 2019-02-24 18:47:04 +00:00
tests
tunefs The size of the UFS soft-updates journal must be a multiple of the 2019-03-02 21:22:56 +00:00
umount umount: remove sync(2) call when used with -f 2018-09-13 13:57:42 +00:00
veriexec Add verifying manifest loader for mac_veriexec 2019-02-26 06:17:23 +00:00
zfsbootcfg DIRDEPS_BUILD: Update dependencies. 2017-10-31 00:07:04 +00:00
Makefile Remove NAND and NANDFS support 2019-06-25 04:50:09 +00:00
Makefile.amd64 NVME support is only for x86 and powerpc64. 2018-06-14 01:15:19 +00:00
Makefile.arm
Makefile.i386 NVME support is only for x86 and powerpc64. 2018-06-14 01:15:19 +00:00
Makefile.inc
Makefile.mips
Makefile.powerpc64 NVME support is only for x86 and powerpc64. 2018-06-14 01:15:19 +00:00
Makefile.sparc64