From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0E8A9C4361B for ; Tue, 15 Dec 2020 04:00:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BE5F1224B1 for ; Tue, 15 Dec 2020 04:00:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725821AbgLOEAG (ORCPT ); Mon, 14 Dec 2020 23:00:06 -0500 Received: from mail.synology.com ([211.23.38.101]:34740 "EHLO synology.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725945AbgLOEAG (ORCPT ); Mon, 14 Dec 2020 23:00:06 -0500 From: ethanwu DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=synology.com; s=123; t=1608004764; bh=eONTHYlh7hFNP11s5x3eVU8vMw7z9yd3I51VV3uawqM=; h=From:To:Cc:Subject:Date; b=cooldKHJQAjaGdZtzD4kVRVwXF4w3OtphUXjhSDAQmML/Ckhds6btxp1OtIF8siRy bQGW0eC0nkjT4o61KF5qjQ/YJCS0FnzQbQMc5eG9v8ifuQRSoODRPxsEKcaqFtupyQ tfrHcn7BK2XY3+kjWbxjSS6m+WOxBTAwoMPx30S0= To: fstests@vger.kernel.org Cc: linux-btrfs@vger.kernel.org, ethanwu Subject: [PATCH v2] btrfs: test if rename handles dir item collision correctly Date: Tue, 15 Dec 2020 11:59:06 +0800 Message-Id: <20201215035906.233272-1-ethanwu@synology.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Synology-MCP-Status: no X-Synology-Spam-Flag: no X-Synology-Spam-Status: score=0, required 6, WHITELIST_FROM_ADDRESS 0 X-Synology-Virus-Status: no Precedence: bulk List-ID: X-Mailing-List: fstests@vger.kernel.org This is a regression test for the issue fixed by the kernel commit titled "btrfs: correctly calculate item size used when item key collision happens" In this case, we'll simply rename many forged filename that cause collision under a directory to see if rename failed and filesystem is forced readonly. Signed-off-by: ethanwu --- v2: - Add a python script to generate the forged name at run-time rather than from hardcoded names - Fix , Btrfs->btrfs, and typo mentioned in v1 src/btrfs_crc32c_forged_name.py | 92 +++++++++++++++++++++++++++++++++ tests/btrfs/228 | 72 ++++++++++++++++++++++++++ tests/btrfs/228.out | 2 + tests/btrfs/group | 1 + 4 files changed, 167 insertions(+) create mode 100755 src/btrfs_crc32c_forged_name.py create mode 100755 tests/btrfs/228 create mode 100644 tests/btrfs/228.out diff --git a/src/btrfs_crc32c_forged_name.py b/src/btrfs_crc32c_forged_name.py new file mode 100755 index 00000000..d8abedde --- /dev/null +++ b/src/btrfs_crc32c_forged_name.py @@ -0,0 +1,92 @@ +# SPDX-License-Identifier: GPL-2.0 + +import struct +import sys +import os +import argparse + +class CRC32(object): + """A class to calculate and manipulate CRC32.""" + def __init__(self): + self.polynom = 0x82F63B78 + self.table, self.reverse = [0]*256, [0]*256 + self._build_tables() + + def _build_tables(self): + for i in range(256): + fwd = i + rev = i << 24 + for j in range(8, 0, -1): + # build normal table + if (fwd & 1) == 1: + fwd = (fwd >> 1) ^ self.polynom + else: + fwd >>= 1 + self.table[i] = fwd & 0xffffffff + # build reverse table =) + if rev & 0x80000000 == 0x80000000: + rev = ((rev ^ self.polynom) << 1) | 1 + else: + rev <<= 1 + rev &= 0xffffffff + self.reverse[i] = rev + + def calc(self, s): + """Calculate crc32 of a string. + Same crc32 as in (binascii.crc32)&0xffffffff. + """ + crc = 0xffffffff + for c in s: + crc = (crc >> 8) ^ self.table[(crc ^ ord(c)) & 0xff] + return crc^0xffffffff + + def forge(self, wanted_crc, s, pos=None): + """Forge crc32 of a string by adding 4 bytes at position pos.""" + if pos is None: + pos = len(s) + + # forward calculation of CRC up to pos, sets current forward CRC state + fwd_crc = 0xffffffff + for c in s[:pos]: + fwd_crc = (fwd_crc >> 8) ^ self.table[(fwd_crc ^ ord(c)) & 0xff] + + # backward calculation of CRC up to pos, sets wanted backward CRC state + bkd_crc = wanted_crc^0xffffffff + for c in s[pos:][::-1]: + bkd_crc = ((bkd_crc << 8) & 0xffffffff) ^ self.reverse[bkd_crc >> 24] + bkd_crc ^= ord(c) + + # deduce the 4 bytes we need to insert + for c in struct.pack('> 24] + bkd_crc ^= ord(c) + + res = s[:pos] + struct.pack('>$seqres.full 2>&1 +_scratch_mount + +# +# In the following for loop, we'll create a leaf fully occupied by +# only one dir item with many forged collision names in it. +# +# leaf 22544384 items 1 free space 0 generation 6 owner FS_TREE +# leaf 22544384 flags 0x1(WRITTEN) backref revision 1 +# fs uuid 9064ba52-3d2c-4840-8e26-35db08fa17d7 +# chunk uuid 9ba39317-3159-46c9-a75a-965ab1e94267 +# item 0 key (256 DIR_ITEM 3737737011) itemoff 25 itemsize 65410 +# ... +# + +$PYTHON2_PROG $here/src/btrfs_crc32c_forged_name.py -d $SCRATCH_MNT -c 310 + +ISRW=$(_fs_options $SCRATCH_DEV | grep -w "rw") +if [ -n "$ISRW" ]; then + echo "FS is Read-Write Test OK" +else + echo "FS is Read-Only. Test Failed" + status=1 + exit +fi + +# success, all done +status=0; exit diff --git a/tests/btrfs/228.out b/tests/btrfs/228.out new file mode 100644 index 00000000..eae514f0 --- /dev/null +++ b/tests/btrfs/228.out @@ -0,0 +1,2 @@ +QA output created by 228 +FS is Read-Write Test OK diff --git a/tests/btrfs/group b/tests/btrfs/group index d18450c7..f8021668 100644 --- a/tests/btrfs/group +++ b/tests/btrfs/group @@ -228,3 +228,4 @@ 224 auto quick qgroup 225 auto quick volume seed 226 auto quick rw snapshot clone prealloc punch +228 auto quick -- 2.25.1