From owner-svn-src-head@freebsd.org Mon Apr 27 02:01:49 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 870262C429B; Mon, 27 Apr 2020 02:01:49 +0000 (UTC) (envelope-from delphij@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 499Sgx34zKz4FVd; Mon, 27 Apr 2020 02:01:49 +0000 (UTC) (envelope-from delphij@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 651FD2B8B; Mon, 27 Apr 2020 02:01:49 +0000 (UTC) (envelope-from delphij@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 03R21nss058860; Mon, 27 Apr 2020 02:01:49 GMT (envelope-from delphij@FreeBSD.org) Received: (from delphij@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 03R21mdA058857; Mon, 27 Apr 2020 02:01:48 GMT (envelope-from delphij@FreeBSD.org) Message-Id: <202004270201.03R21mdA058857@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: delphij set sender to delphij@FreeBSD.org using -f From: Xin LI Date: Mon, 27 Apr 2020 02:01:48 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r360359 - head/sbin/fsck_msdosfs X-SVN-Group: head X-SVN-Commit-Author: delphij X-SVN-Commit-Paths: head/sbin/fsck_msdosfs X-SVN-Commit-Revision: 360359 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 27 Apr 2020 02:01:49 -0000 Author: delphij Date: Mon Apr 27 02:01:48 2020 New Revision: 360359 URL: https://svnweb.freebsd.org/changeset/base/360359 Log: Fix a bug with dirty file system handling. r356313 broke handling of dirty file system because we have restricted the correction of "odd" byte sequences to checkfat(), and as a result the dirty bit is never cleared. The old fsck_msdosfs code would write FAT twice to fix the dirty bit, which is also not ideal. Fix this by introducing a new rountine, cleardirty() which will perform the set of clean bit only, and use it in checkfilesys() if we thought the file system was dirty. Reviewed by: cem, emaste MFC after: 3 day Differential Revision: https://reviews.freebsd.org/D24581 Modified: head/sbin/fsck_msdosfs/check.c head/sbin/fsck_msdosfs/ext.h head/sbin/fsck_msdosfs/fat.c Modified: head/sbin/fsck_msdosfs/check.c ============================================================================== --- head/sbin/fsck_msdosfs/check.c Sun Apr 26 22:08:47 2020 (r360358) +++ head/sbin/fsck_msdosfs/check.c Mon Apr 27 02:01:48 2020 (r360359) @@ -169,7 +169,7 @@ checkfilesys(const char *fname) if (mod & FSDIRTY) { pwarn("MARKING FILE SYSTEM CLEAN\n"); - mod |= writefat(fat); + mod |= cleardirty(fat); } else { pwarn("\n***** FILE SYSTEM IS LEFT MARKED AS DIRTY *****\n"); mod |= FSERROR; /* file system not clean */ Modified: head/sbin/fsck_msdosfs/ext.h ============================================================================== --- head/sbin/fsck_msdosfs/ext.h Sun Apr 26 22:08:47 2020 (r360358) +++ head/sbin/fsck_msdosfs/ext.h Mon Apr 27 02:01:48 2020 (r360359) @@ -90,6 +90,8 @@ int writefsinfo(int, struct bootblock *); /* Opaque type */ struct fat_descriptor; +int cleardirty(struct fat_descriptor *); + void fat_clear_cl_head(struct fat_descriptor *, cl_t); bool fat_is_cl_head(struct fat_descriptor *, cl_t); Modified: head/sbin/fsck_msdosfs/fat.c ============================================================================== --- head/sbin/fsck_msdosfs/fat.c Sun Apr 26 22:08:47 2020 (r360358) +++ head/sbin/fsck_msdosfs/fat.c Mon Apr 27 02:01:48 2020 (r360359) @@ -578,7 +578,6 @@ valid_cl(struct fat_descriptor *fat, cl_t cl) * h = hard error flag (1 = ok; 0 = I/O error) * x = any value ok */ - int checkdirty(int fs, struct bootblock *boot) { @@ -636,6 +635,53 @@ checkdirty(int fs, struct bootblock *boot) if ((buffer[7] & 0x0c) == 0x0c) ret = 1; } + +err: + free(buffer); + return ret; +} + +int +cleardirty(struct fat_descriptor *fat) +{ + int fd, ret = FSERROR; + struct bootblock *boot; + u_char *buffer; + size_t len; + off_t off; + + boot = boot_of_(fat); + fd = fd_of_(fat); + + if (boot->ClustMask != CLUST16_MASK && boot->ClustMask != CLUST32_MASK) + return 0; + + off = boot->bpbResSectors; + off *= boot->bpbBytesPerSec; + + buffer = malloc(len = boot->bpbBytesPerSec); + if (buffer == NULL) { + perr("No memory for FAT sectors (%zu)", len); + return 1; + } + + if ((size_t)pread(fd, buffer, len, off) != len) { + perr("Unable to read FAT"); + goto err; + } + + if (boot->ClustMask == CLUST16_MASK) { + buffer[3] |= 0x80; + } else { + buffer[7] |= 0x08; + } + + if ((size_t)pwrite(fd, buffer, len, off) != len) { + perr("Unable to write FAT"); + goto err; + } + + ret = FSOK; err: free(buffer);