From patchwork Tue Jul 12 22:33:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heinrich Schuchardt X-Patchwork-Id: 1655692 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=DzPX2JWZ; dkim-atps=neutral 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=) 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 (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LjFsb1JQkz9ryY for ; Wed, 13 Jul 2022 08:33:55 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 03B06841F5; Wed, 13 Jul 2022 00:33:35 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (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="DzPX2JWZ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id AB923841EB; Wed, 13 Jul 2022 00:33:27 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,FREEMAIL_FROM,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) (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 3D51B84160 for ; Wed, 13 Jul 2022 00:33:22 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (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=1657665200; bh=rph8rLpfBTkAJ0UocRGqJGB1zXbiktxhaYfa+QwhsqE=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=DzPX2JWZJIAbaaZ9xKmrc6PeEKHa4Xx321ueIAfmRYPMLJ9i5KmRferF/2t6e1BW5 yE1pbsO9wNvfP3pO5/2uYB9t0+CrIVdTWff6tt6VcN0tmfMnxzAzpMheorI2aJbPcy 1+WdGBrFu66kZtHredbM0cGhocx2hEJF9KCL4l0Q= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from rpi3.fritz.box ([62.143.94.109]) by mail.gmx.net (mrgmx105 [212.227.17.174]) with ESMTPSA (Nemesis) id 1Ma24y-1o5lkC1FPo-00VvJH; Wed, 13 Jul 2022 00:33:20 +0200 From: Heinrich Schuchardt To: Tom Rini Cc: AKASHI Takahiro , u-boot@lists.denx.de, Heinrich Schuchardt Subject: [PATCH 1/3] fs: fat: finding an empty FAT cluster Date: Tue, 12 Jul 2022 22:33:12 +0000 Message-Id: <20220712223314.20530-2-xypron.glpk@gmx.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220712223314.20530-1-xypron.glpk@gmx.de> References: <20220712223314.20530-1-xypron.glpk@gmx.de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bQLi/tIpvKGu5RkiP+EOODidtt6pqqgnJVDHyRiBhF3r Y0w6IYAI7EuNE3ojIIqJvFpZPs6UwniaLcFO75XqXZBDv6EDEeEqexothfROg4nFyBE5i8KX3 wnPNIPWruFNY/HXlL+9wWxX9hQ83w5e7ilk9XGttAolnp2tEto59ftJZNbqmEfXEXabZiA5oc A1K9janvgNLYj6KoT9OF8oBg/cVsxAXVjvYs0Bj4XLbq3G0WLOXSbvocuALQPeH6wt0mOxDzC mWcv/a+P2DqlMxw2d5ykfgeRwMONcIUQ5SIcPA8T26iaei91jphov1uND0YKXzd0nE8vtGAMK xby03cPWHk/7BFBXufkE/VB2xQKzt1Id65n8rou1/LqiDy4VCtgEG90SQ== X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 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.103.6 at phobos.denx.de X-Virus-Status: Clean Currently we have two functions with redundant coding to find an empty cluster: * find_empty_cluster() seeks from the beginning of the FAT table * determine_fatent() seeks after a given entry Both do not detect the end of the FAT table correctly and return an invalid cluster number if no empty entry if found. find_empty_cluster() is replaced by an invocation of determine_fatent(). determine_fatent() is changed to seek in a second round from the beginning of the FAT table and to return an error code if no free entry is found. With this patch we will always find an empty cluster if it exists. Further patches are needed to handle the disk full error gracefully. Signed-off-by: Heinrich Schuchardt --- fs/fat/fat_write.c | 56 ++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 27 deletions(-) -- 2.30.2 diff --git a/fs/fat/fat_write.c b/fs/fat/fat_write.c index 8ff2f6def0..a137e14f41 100644 --- a/fs/fat/fat_write.c +++ b/fs/fat/fat_write.c @@ -536,22 +536,41 @@ static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value) return 0; } -/* - * Determine the next free cluster after 'entry' in a FAT (12/16/32) table - * and link it to 'entry'. EOC marker is not set on returned entry. +/** + * determine_fatent() - get next free FAT cluster + * + * The parameter @entry indicates the current cluster. To reduce fragementation + * the function first searches for a free cluster after the current cluster. + * If none is found, the search is repeated from the beginning of the FAT table. + * + * If @entry is set, the new FAT entry is appended to the given one. + * If @entry is zero, only the number of the first free cluster is returned. + * + * @entry: current entry + * Return: next free cluster or negative error */ -static __u32 determine_fatent(fsdata *mydata, __u32 entry) +static int determine_fatent(fsdata *mydata, __u32 entry) { - __u32 next_fat, next_entry = entry + 1; + __u32 next_fat, next_entry = entry; + int second_round = 0; while (1) { + ++next_entry; + if (CHECK_CLUST(next_entry, mydata->fatsize)) { + if (!second_round) { + second_round = 1; + next_entry = 3; + } else { + return -ENOSPC; + } + } next_fat = get_fatent(mydata, next_entry); - if (next_fat == 0) { + if (!next_fat) { /* found free entry, link to entry */ - set_fatent_value(mydata, entry, next_entry); + if (entry) + set_fatent_value(mydata, entry, next_entry); break; } - next_entry++; } debug("FAT%d: entry: %08x, entry_value: %04x\n", mydata->fatsize, entry, next_entry); @@ -794,23 +813,6 @@ get_set_cluster(fsdata *mydata, __u32 clustnum, loff_t pos, __u8 *buffer, return 0; } -/* - * Find the first empty cluster - */ -static int find_empty_cluster(fsdata *mydata) -{ - __u32 fat_val, entry = 3; - - while (1) { - fat_val = get_fatent(mydata, entry); - if (fat_val == 0) - break; - entry++; - } - - return entry; -} - /** * new_dir_table() - allocate a cluster for additional directory entries * @@ -824,7 +826,7 @@ static int new_dir_table(fat_itr *itr) int dir_oldclust = itr->clust; unsigned int bytesperclust = mydata->clust_size * mydata->sect_size; - dir_newclust = find_empty_cluster(mydata); + dir_newclust = determine_fatent(mydata, 0); /* * Flush before updating FAT to ensure valid directory structure @@ -1066,7 +1068,7 @@ set_clusters: /* Assure that curclust is valid */ if (!curclust) { - curclust = find_empty_cluster(mydata); + curclust = determine_fatent(mydata, 0); set_start_cluster(mydata, dentptr, curclust); } else { newclust = get_fatent(mydata, curclust); From patchwork Tue Jul 12 22:33:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heinrich Schuchardt X-Patchwork-Id: 1655691 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=Rb2o2pX1; dkim-atps=neutral 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=) 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 (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LjFsN4WByz9ryY for ; Wed, 13 Jul 2022 08:33:44 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 8BF34841E9; Wed, 13 Jul 2022 00:33:30 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (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="Rb2o2pX1"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id E0122841DD; Wed, 13 Jul 2022 00:33:24 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,FREEMAIL_FROM,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mout.gmx.net (mout.gmx.net [212.227.17.21]) (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 2A0438415E for ; Wed, 13 Jul 2022 00:33:22 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (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=1657665200; bh=Y8rHqH7SlYPEPf7BkdiNXMqVUEARweay/arumKiYp44=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=Rb2o2pX1+VAUCgWh5bcuhpS0F02sBUeBOMXWY+L3XhoksKskgPiS4iScXbHayFWj2 Hl7+SacrV9ELaPFaENw7XvRW8k4YPPIcUXQGSLovJt+YeumiO4XVInrQGIZS87SvV7 r8jFiYm1oEk971l6QJI6sWS6UsETD0Xp33vA+UwI= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from rpi3.fritz.box ([62.143.94.109]) by mail.gmx.net (mrgmx105 [212.227.17.174]) with ESMTPSA (Nemesis) id 1MXGvG-1o47qw2Hss-00YfOv; Wed, 13 Jul 2022 00:33:20 +0200 From: Heinrich Schuchardt To: Tom Rini Cc: AKASHI Takahiro , u-boot@lists.denx.de, Heinrich Schuchardt Subject: [PATCH 2/3] fs: fat: determine_fatent() error handling Date: Tue, 12 Jul 2022 22:33:13 +0000 Message-Id: <20220712223314.20530-3-xypron.glpk@gmx.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220712223314.20530-1-xypron.glpk@gmx.de> References: <20220712223314.20530-1-xypron.glpk@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:y9iACw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HqV93QHEZ 0WxvKR3Kdls60INuoIzVQ/uF1aeON1oxVnKHTpEXquSTO+9akfC5PfKBAcU7BT0sqGAVhfd/u un6HqG7nBuSn4EiQkYmO81wl6lmvEVX7s6xp7zvLdjUz14k/DfvgMXb04QK5W8UUHGpMvsuP6 OZ6KnE++YofUWFzakvEglGBEn7L0tBAES0SEd9LcoJ8jBh5rumXM/uCszZPbNlril41wPay3e 5VSSiIBnyyaQT4xUXqCa8vifzKp3kfJVXphSjVbK2QX7PPuLvgNojFuSihJ4uAuww9476go8d AjOgykDrSh+8ZvXJkHrIMnOfujwetXdAvDFO/dBqX0/P6kkOAVaqorclQ== X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 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.103.6 at phobos.denx.de X-Virus-Status: Clean Handle disk full errors. Signed-off-by: Heinrich Schuchardt --- fs/fat/fat_write.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) -- 2.30.2 diff --git a/fs/fat/fat_write.c b/fs/fat/fat_write.c index a137e14f41..57522f96a8 100644 --- a/fs/fat/fat_write.c +++ b/fs/fat/fat_write.c @@ -827,6 +827,8 @@ static int new_dir_table(fat_itr *itr) unsigned int bytesperclust = mydata->clust_size * mydata->sect_size; dir_newclust = determine_fatent(mydata, 0); + if (dir_newclust < 0) + return dir_newclust; /* * Flush before updating FAT to ensure valid directory structure @@ -927,8 +929,8 @@ set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer, loff_t maxsize, loff_t *gotsize) { unsigned int bytesperclust = mydata->clust_size * mydata->sect_size; - __u32 curclust = START(dentptr); - __u32 endclust = 0, newclust = 0; + int curclust = START(dentptr); + int endclust = 0, newclust = 0; u64 cur_pos, filesize; loff_t offset, actsize, wsize; @@ -1069,12 +1071,16 @@ set_clusters: /* Assure that curclust is valid */ if (!curclust) { curclust = determine_fatent(mydata, 0); + if (curclust < 0) + return -1; set_start_cluster(mydata, dentptr, curclust); } else { newclust = get_fatent(mydata, curclust); if (IS_LAST_CLUST(newclust, mydata->fatsize)) { newclust = determine_fatent(mydata, curclust); + if (newclust < 0) + return -1; set_fatent_value(mydata, curclust, newclust); curclust = newclust; } else { @@ -1095,6 +1101,8 @@ set_clusters: /* search for consecutive clusters */ while (actsize < filesize) { newclust = determine_fatent(mydata, endclust); + if (newclust < 0) + return -1; if ((newclust - 1) != endclust) /* write to */ From patchwork Tue Jul 12 22:33:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heinrich Schuchardt X-Patchwork-Id: 1655693 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=fHEATPIN; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LjFsp5Fv0z9ryY for ; Wed, 13 Jul 2022 08:34:06 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id C19A6841F6; Wed, 13 Jul 2022 00:33:39 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (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="fHEATPIN"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2F22084160; Wed, 13 Jul 2022 00:33:28 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,FREEMAIL_FROM,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mout.gmx.net (mout.gmx.net [212.227.17.20]) (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 859E0841E9 for ; Wed, 13 Jul 2022 00:33:22 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (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=1657665201; bh=VM8+yW38F2hzpx7GcBHQY6y0RyBFdjPGFi9p1AxZ1R0=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=fHEATPINOjVuf0F+Y6no0m9lHl92gcQkA95rhT15R3b6N0GT4OhDB7DhA6OYqDcTD lhNyoAaKuYCVmXKr2f9Hl6gfJeUtXbY6CBSHvPf3yLbBwfPqd5bWVKd+Z2nB6Ei0Nc AIUUi8Mxs/NvlMm90M6ilO6AEhP+36dGRaFm9PTU= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from rpi3.fritz.box ([62.143.94.109]) by mail.gmx.net (mrgmx105 [212.227.17.174]) with ESMTPSA (Nemesis) id 1N8ofO-1nWytK3Gic-015oI9; Wed, 13 Jul 2022 00:33:20 +0200 From: Heinrich Schuchardt To: Tom Rini Cc: AKASHI Takahiro , u-boot@lists.denx.de, Heinrich Schuchardt Subject: [PATCH 3/3] fs: fat: carve out fat_create_dir_entry() Date: Tue, 12 Jul 2022 22:33:14 +0000 Message-Id: <20220712223314.20530-4-xypron.glpk@gmx.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220712223314.20530-1-xypron.glpk@gmx.de> References: <20220712223314.20530-1-xypron.glpk@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:DgFSxx3r96Mo9KATtfzarRe/Do01nCj8BmZOKRv6ziHtxjvKfQg 6wARzic6UkL28IkPzVzle/2zvR+Ee5XfZtgf17vZRqpEkbSfgISvGrghspKEg7exP4kWR23 nITVfhK17FmtnzsZxZ3Ru2wnD3SeqXbSow8hWeRVdhPs8wi1jzgeXHDXynWS7mqURYR/upW rTyLxFRxX0ooo0aCAkMRA== X-UI-Out-Filterresults: notjunk:1;V03:K0:cyWKr3038aQ=:rsGi3iyXQc0vh4ygl7WcOD VWJpe+6O5ebuP0ku+hZj5f6iCeTiusTUWM2+O1UlxW3F5f+4L0k6Pd8YqZ5HUPTIlumdUalMO kH656NcOdhx1aXTcqPppIFfZxjDz+c932pFVjk1xOlCo/vCFWzYtS8UOTVbbBygzjO8quHhVF Mk7zGSHKeFCGZrHRX29zXfEIF4zecxCQhNhUJoSFzs+Z7ZWIFWF3wYo/XGfz3syEs1IZyX0AY hCIOYv075t2sAmWj5AZVF9IMzEfQ19waon5QDMrkvjlKkkUOUUqciwcWEd5gHd6E2i9OCj1oe iRVqxk0A8OmZETzHZSu4sEGIxEpKnjXwv+W9mRacNSROvrg2MUkcC+Z1LUGsMQ4H5jAIKuoBd ym1QKjFBZ/O+2Y7iNoVgVXCj5yA7NbTicFb4tI9IfQhBBrJT7Ed2uBTqO8gdDngtwxXjFQ9WH WfmQPla0m6X2L8gspRKip0O7M1OVZ2pLGg90ot3r68YDIicnlzXsNmh0dkqohcbRBseIvNPXw eAXlSfR1LkjIHSmIRaHQavzAm3DS4dvMoVJgjE+tHiUDQpP+36wX0hJ1f1tkFVX7GiKTqEvWw qRaigGIY5tsb4hkIQDBx4KX6P5VxvGIkW8E9owJXCrTY4fNp7ql7c1ZwdOutiMusK8QGxaFxp ugq5NFCpAK0+ZAMzT+RlqI7d/Eeg7IwQyGmjyPoIZFBuXKN0d5OFu7lRE+MfMoOBpLhEJ+qdQ uBR0FqoVofZFARleiz+nB64CaYAf9vYdBAtIDDHXPuy3Xsb7nNv4/HqqzAN6R/wQdeBQcqUtv bjatd8cS63yFCadBVh5j2OZXuADsID25a4X4ndDpQfM7L+62HlGOtgeykfeFg3rSWpDZULxFs BY9QP0nWWE8Bb2IKxEpyNe+aD4tX9GlmcUYHJBsrw9xC3RYfINeAWzKdhMOTQ/WRD3syDxAEB /elMs3Ysh2Aybx3uAivcgFdg3jpxioNSI4MQpBRuXrX8oPWFoWPYedXAfHKwFoyRRmjjuuvNx R9DNudD3FGxpDAHJKrxr04YBVSTwlvtL18WfN5w1d/qHqEnqaHU/j83MEpCKTIjvOl7WSoFkk FTfbOuzb9KCRVYFVbjt4mZ0WhPerZ8ykOMcL4BBMqXCxVus6wtkx8vdIg== X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 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.103.6 at phobos.denx.de X-Virus-Status: Clean fat_mkdir() and file_fat_write_at() use identical code to create a new directory entry. Carve out a new function fat_create_dir_entry() to avoid this code duplication. Signed-off-by: Heinrich Schuchardt --- fs/fat/fat_write.c | 93 ++++++++++++++++++++-------------------------- 1 file changed, 40 insertions(+), 53 deletions(-) -- 2.30.2 diff --git a/fs/fat/fat_write.c b/fs/fat/fat_write.c index 57522f96a8..a25b2283d4 100644 --- a/fs/fat/fat_write.c +++ b/fs/fat/fat_write.c @@ -1314,6 +1314,43 @@ static int normalize_longname(char *l_filename, const char *filename) return 0; } +/** + * fat_create_dir_entry() - create directory entry + * + * @itr: directory iterator + * @basename: name of file or directory to be created + * @size: file size + * @attr: file or directory attributes + * Return: 0 for success, -EIO on error + */ +static int fat_create_dir_entry(fat_itr *itr, const char *basename, + loff_t size, u8 attr) +{ + /* Create a new file */ + char shortname[SHORT_NAME_SIZE]; + int ndent; + int ret; + + /* Check if long name is needed */ + ndent = set_name(itr, basename, shortname); + if (ndent < 0) + return ndent; + ret = fat_find_empty_dentries(itr, ndent); + if (ret) + return ret; + if (ndent > 1) { + /* Set long name entries */ + ret = fill_dir_slot(itr, basename, shortname); + if (ret) + return ret; + } + + /* Set short name entry */ + fill_dentry(itr->fsdata, itr->dent, shortname, 0, size, attr); + + return 0; +} + int file_fat_write_at(const char *filename, loff_t pos, void *buffer, loff_t size, loff_t *actwrite) { @@ -1383,8 +1420,6 @@ int file_fat_write_at(const char *filename, loff_t pos, void *buffer, retdent->size = cpu_to_le32(pos + size); } else { /* Create a new file */ - char shortname[SHORT_NAME_SIZE]; - int ndent; if (pos) { /* No hole allowed */ @@ -1392,25 +1427,7 @@ int file_fat_write_at(const char *filename, loff_t pos, void *buffer, goto exit; } - /* Check if long name is needed */ - ndent = set_name(itr, basename, shortname); - if (ndent < 0) { - ret = ndent; - goto exit; - } - ret = fat_find_empty_dentries(itr, ndent); - if (ret) - goto exit; - if (ndent > 1) { - /* Set long name entries */ - ret = fill_dir_slot(itr, basename, shortname); - if (ret) - goto exit; - } - - /* Set short name entry */ - fill_dentry(itr->fsdata, itr->dent, shortname, 0, size, - ATTR_ARCH); + ret = fat_create_dir_entry(itr, basename, size, ATTR_ARCH); retdent = itr->dent; } @@ -1693,38 +1710,8 @@ int fat_mkdir(const char *dirname) ret = -EEXIST; goto exit; } else { - char shortname[SHORT_NAME_SIZE]; - int ndent; - - if (itr->is_root) { - /* root dir cannot have "." or ".." */ - if (!strcmp(l_dirname, ".") || - !strcmp(l_dirname, "..")) { - ret = -EINVAL; - goto exit; - } - } - - /* Check if long name is needed */ - ndent = set_name(itr, basename, shortname); - if (ndent < 0) { - ret = ndent; - goto exit; - } - ret = fat_find_empty_dentries(itr, ndent); - if (ret) - goto exit; - if (ndent > 1) { - /* Set long name entries */ - ret = fill_dir_slot(itr, basename, shortname); - if (ret) - goto exit; - } - - /* Set attribute as archive for regular file */ - fill_dentry(itr->fsdata, itr->dent, shortname, 0, 0, - ATTR_DIR | ATTR_ARCH); - + ret = fat_create_dir_entry(itr, basename, 0, + ATTR_DIR | ATTR_ARCH); retdent = itr->dent; }