freebsd-dev/sys/fs/msdosfs
Kyle Evans 5fdac75222 msdosfs: do not deget unlinked denodes
When a file is unlinked, the denode is not reclaimed until the last
reference is dropped, but the directory entry is immediately up for reuse.
This is a problem later when createde goes to grab a denode for the newly
created entry -- we search the hash and find a dead denode, then return that
without even bumping the reference count and the data later gets truncated
when the the last reference to the unlinked file is dropped.

This manifested itself as a broken in-place strip(1) on msdosfs. elfcopy
will do a sequence incredibly roughly like this:

open("/mnt/foo", ...) => fd 3
mmap()
unlink("/mnt/foo")
open("/mnt/foo", ...) => fd 4
write(4, ...)
close(4)
close(3)

and the resulting file would be truncated, but the write succeeded, as long
as a reference to the unlinked file had not been closed.

Some archaeology indicates that this bug has likely existed since msdosfs
was converted to use vfs_hash instead of a home rolled hash implementation
in r143570. Prior to that point, the hashget implementation would do a
refcnt check while searching and explicitly only return a denode with
de_refcnt != 0. vfs_hash did not yet have the callback that it does today,
so this slipped away and did not come back when it later grew that
functionality.

The comment indicating that we want to skip these denodes has been updated
to reflect where this is actually done. My repo-diving session seems to
indicate that the refcnt check was likely never actually below the comment,
to be pedantic, but instead a detail wrapped up in the hashget
implementation since the beginning of its inclusion into FreeBSD.

This bug was the cause behind the issue addressed in r352557.

Reported by:	jhibbits
Reviewed by:	kib
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D21731
2019-09-20 20:47:10 +00:00
..
bootsect.h msdosfs: use C99 types 2017-05-19 18:13:41 +00:00
bpb.h msdosfs: use C99 types 2017-05-19 18:13:41 +00:00
denode.h makefs: share denode.h between kernel msdosfs and makefs 2019-08-21 19:07:13 +00:00
direntry.h Revert msdosfs MAKEFS #ifdef changes from r319870 2018-07-24 21:10:17 +00:00
fat.h buf: Add B_INVALONERR flag to discard data 2019-09-11 21:24:14 +00:00
msdosfs_conv.c msdosfs: trim EOL whitespace 2018-07-31 12:44:28 +00:00
msdosfs_denode.c msdosfs: do not deget unlinked denodes 2019-09-20 20:47:10 +00:00
msdosfs_fat.c buf: Add B_INVALONERR flag to discard data 2019-09-11 21:24:14 +00:00
msdosfs_iconv.c sys/fs: further adoption of SPDX licensing ID tags. 2017-11-27 15:15:37 +00:00
msdosfs_lookup.c msdosfs: Remove redundant brelse() after r294954 2019-09-06 08:08:10 +00:00
msdosfs_vfsops.c buf: Add B_INVALONERR flag to discard data 2019-09-11 21:24:14 +00:00
msdosfs_vnops.c msdosfs: Remove redundant brelse() after r294954 2019-09-06 08:08:10 +00:00
msdosfsmount.h msdosfsmount.h: fix ifdef comment 2019-09-09 18:35:17 +00:00