From patchwork Sun Nov 29 02:02:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heinrich Schuchardt X-Patchwork-Id: 1407776 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gmx.de Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=gmx.net header.i=@gmx.net header.a=rsa-sha256 header.s=badeba3b8450 header.b=HwgTIqGm; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CkBWQ6h1Kz9sSf for ; Sun, 29 Nov 2020 13:04:34 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 6D0B382798; Sun, 29 Nov 2020 03:03:22 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; secure) header.d=gmx.net header.i=@gmx.net header.b="HwgTIqGm"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 98BC182750; Sun, 29 Nov 2020 03:02:57 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mout.gmx.net (mout.gmx.net [212.227.15.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id BEF3482750 for ; Sun, 29 Nov 2020 03:02:43 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=xypron.glpk@gmx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1606615354; bh=/rZzV6KyNMDRKg6tEOMyFK40orgSgAobqHCMvEnCkEY=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=HwgTIqGmTBJ+5k88sFjHIjRheNilQX7LaFy7TcDIhUtBwj4EdAuejGuP7MZfV/SpF o1keTJbMSuzQ3wgL3Tgh0NqJBC7178eYoiLFmRd48yWn8yuh92PKnZHRdkMsZUSbJS wR6Wq5XhSb9VN0nTM/4dhUkhvpIMPK7IZzDZkfuE= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from mcbin.fritz.box ([62.143.246.89]) by mail.gmx.com (mrgmx005 [212.227.17.184]) with ESMTPSA (Nemesis) id 1MbzuH-1kCceD2riZ-00dXZS; Sun, 29 Nov 2020 03:02:34 +0100 From: Heinrich Schuchardt To: Tom Rini Cc: Simon Glass , Christian Gmeiner , Jason Wessel , AKASHI Takahiro , Marek Szyprowski , Bin Meng , Reuben Dowle , Marcin Juszkiewicz , Thomas Hebb , Samer El-Haj-Mahmoud , u-boot@lists.denx.de, Heinrich Schuchardt Subject: [PATCH 12/18] fs: fat: flush new directory cluster Date: Sun, 29 Nov 2020 03:02:10 +0100 Message-Id: <20201129020216.4865-13-xypron.glpk@gmx.de> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201129020216.4865-1-xypron.glpk@gmx.de> References: <20201129020216.4865-1-xypron.glpk@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:akaMEyoknLBzfn8elOMBSl37BYLo/9tfFgj9DQNTEW1xYLJxEkh rPU0rTRAtv8tvzeE20rcxeQoG/6gKD9Ev8qaziHApcosUi3txqs2drHJELjCPG5AJ69umOg kxiyxTe+CdjfNph+ytBj0n7O5Muzoxf4Vov+loi0i/da/mIaO9k02Htb5512alIG7qgsoa4 a0GyjA4+K46nX58BOqNGA== X-UI-Out-Filterresults: notjunk:1;V03:K0:AApAhb0zRUY=:li8B/o8phVmlJjJ5AX8c1D vvDx61JLlQuP7MRkpIOh7944eBIqaTuhw4IyOZzu/QOVSEq5XJ06xt1qGG5mnKWugcQpL7B7T 9vA1pH+/AJYmtnqf70fVP07KyrljJudpzl+24egsxunkw3TuIiAdve5E6SSc1KD4VEHNgTw// QLKe1shRPksC8AX7rtrNRRoQFgC6gU6bP0iLOnbCUxu9HfEP/IuYzVP9bb9oIp1UHDEYOczQP khmwHT8hjjf8LyZMGTC9ziM+UwaIA5Ne/cnYKbyw13TVCbMq8EQqHLPxBXsXuzI+3f6dbiIh7 53ZQCQ9YuEXmvdWWuPJKXwvEuN2ehdfMNQc6rmXsgjNGR2JXnGRVTjny52mzATo1wxEVRUSc0 hDj2i8cifdPSp6p6itGEuDhFSmEoAUPJCyLdlMdnMbT4seasKltHyqCsjUWiEFumqjRz1HLr2 VnYkxjQSXFEdNMiHEopJ1OhTMwFUHcke+ElxIoPEqeO4RhT7ohOMSvm8FPFFPJ0ueeLZOHJ/W wsrRiJLTfVj/FEehK7QvLwpf+U68ZBrKW6a7s79VE6Ub3V3Wvx1zJ1rQ9nyLayheugc3IqUDx zd5+6A03E9XnjHhs0YS7bE4WcqWzv9hE90tVxhyzw+9N2OPhZCqfEP5MVhrlyu4WOUGJNpEsp bn8/uv97o6AKHrcNL5EhCdLn/08rwWxpmE8R8sKs3m20AzJ7te5e1Im2aNMnYM516aX32KJJu O46w++A3sBLJy4BSwSstW+To2gmuOGs6gdKa4n+sfblnz+Aby+giH6qRTyV8IL+gdCHEAk8NI F0+RyALoWHqjBtQbMPwzUQKEpNKREwAeAQFM5cVOcJ13nlZeuCnIYLok3r5Im+ksu+Jz0MLWw gVItOhsyo0eyvxl7+5xw== X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.3 at phobos.denx.de X-Virus-Status: Clean When handling long file names directory entries may be split over multiple clusters. We must make sure that new clusters are zero filled on disk. When allocating a new cluster for a directory flush it. The flushing should be executed before updating the FAT. This way if flushing fails, we still have a valid directory structure. Signed-off-by: Heinrich Schuchardt --- fs/fat/fat_write.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) -- 2.29.2 diff --git a/fs/fat/fat_write.c b/fs/fat/fat_write.c index 0746d73f8d..941f8789ab 100644 --- a/fs/fat/fat_write.c +++ b/fs/fat/fat_write.c @@ -738,17 +738,32 @@ static int find_empty_cluster(fsdata *mydata) return entry; } -/* - * Allocate a cluster for additional directory entries +/** + * new_dir_table() - allocate a cluster for additional directory entries + * + * @itr: directory iterator + * Return: 0 on success, -EIO otherwise */ static int new_dir_table(fat_itr *itr) { fsdata *mydata = itr->fsdata; int dir_newclust = 0; + int dir_oldclust = itr->clust; unsigned int bytesperclust = mydata->clust_size * mydata->sect_size; dir_newclust = find_empty_cluster(mydata); - set_fatent_value(mydata, itr->clust, dir_newclust); + + /* + * Flush before updating FAT to ensure valid directory structure + * in case of failure. + */ + itr->clust = dir_newclust; + itr->next_clust = dir_newclust; + memset(itr->block, 0x00, bytesperclust); + if (flush_dir(itr)) + return -EIO; + + set_fatent_value(mydata, dir_oldclust, dir_newclust); if (mydata->fatsize == 32) set_fatent_value(mydata, dir_newclust, 0xffffff8); else if (mydata->fatsize == 16) @@ -756,13 +771,8 @@ static int new_dir_table(fat_itr *itr) else if (mydata->fatsize == 12) set_fatent_value(mydata, dir_newclust, 0xff8); - itr->clust = dir_newclust; - itr->next_clust = dir_newclust; - if (flush_dirty_fat_buffer(mydata) < 0) - return -1; - - memset(itr->block, 0x00, bytesperclust); + return -EIO; itr->dent = (dir_entry *)itr->block; itr->last_cluster = 1;