From patchwork Wed Jan 31 10:25:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Wang X-Patchwork-Id: 1893360 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=N6u/u9xx; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=patchwork.ozlabs.org) Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TPyrL3Qn8z1yQ0 for ; Wed, 31 Jan 2024 21:26:42 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 3DE673CF922 for ; Wed, 31 Jan 2024 11:26:40 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-6.smtp.seeweb.it (in-6.smtp.seeweb.it [IPv6:2001:4b78:1:20::6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by picard.linux.it (Postfix) with ESMTPS id 45A5A3CA8CA for ; Wed, 31 Jan 2024 11:25:31 +0100 (CET) Authentication-Results: in-6.smtp.seeweb.it; spf=pass (sender SPF authorized) smtp.mailfrom=redhat.com (client-ip=170.10.129.124; helo=us-smtp-delivery-124.mimecast.com; envelope-from=liwang@redhat.com; receiver=lists.linux.it) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 in-6.smtp.seeweb.it (Postfix) with ESMTPS id 05F0A1409517 for ; Wed, 31 Jan 2024 11:25:30 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706696729; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HgP536nsY+EyzA6vAsIOoH/p8ASXIJFc5vTaT5YnoGo=; b=N6u/u9xxPM+/j3LZfZ8PfwMYwWtMBsAGV2G4TYHfu413Pu+0e7mjZk5heITZJaTgSyLz9N Ldu+cR8ir9XAZLcw+6LaqmO+iyoaEv3EwG6qihtcegoifyJEqxDzyi96LNV5VtY30oWNzY Bo+kvwKdz5ftxyYiLX1AQcQqSKWDmwQ= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-609-9Z6ElE3_NUid_PYY75xjwQ-1; Wed, 31 Jan 2024 05:25:28 -0500 X-MC-Unique: 9Z6ElE3_NUid_PYY75xjwQ-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id E849410AFB85; Wed, 31 Jan 2024 10:25:27 +0000 (UTC) Received: from liwang-workstation.lab.eng.nay.redhat.com (unknown [10.66.145.229]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B553A157; Wed, 31 Jan 2024 10:25:26 +0000 (UTC) From: Li Wang To: ltp@lists.linux.it Date: Wed, 31 Jan 2024 18:25:11 +0800 Message-Id: <20240131102514.2739270-6-liwang@redhat.com> In-Reply-To: <20240131102514.2739270-1-liwang@redhat.com> References: <20240131102514.2739270-1-liwang@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE shortcircuit=no autolearn=disabled version=4.0.0 X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on in-6.smtp.seeweb.it X-Virus-Scanned: clamav-milter 1.0.3 at in-6.smtp.seeweb.it X-Virus-Status: Clean Subject: [LTP] [Patch v6 5/8] libswap: Introduce file contiguity check X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" This patch introduces a new function file_is_contiguous to the libltpswap library to determine if a swap file is stored in a contiguous block of disk space, which is a typical requirement for swap files in Linux. The function performs a series of checks using the fiemap structure to assess the contiguity of the file and logs the result. It is integrated into the is_swap_supported function to replace the previous tst_fibmap check, providing a more reliable method for verifying that a file suitable for swap is indeed contiguous. Signed-off-by: Li Wang Reviewed-by: Petr Vorel Tested-by: Petr Vorel --- libs/libltpswap/libswap.c | 70 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 5 deletions(-) diff --git a/libs/libltpswap/libswap.c b/libs/libltpswap/libswap.c index e606e3f03..95d4f59b0 100644 --- a/libs/libltpswap/libswap.c +++ b/libs/libltpswap/libswap.c @@ -6,6 +6,8 @@ #include #include +#include +#include #define TST_NO_DEFAULT_MAIN @@ -69,6 +71,61 @@ static int prealloc_contiguous_file(const char *path, size_t bs, size_t bcount) return 0; } +static int file_is_contiguous(const char *filename) +{ + int fd, contiguous = 0; + struct fiemap *fiemap; + + if (tst_fibmap(filename) == 0) { + contiguous = 1; + goto out; + } + + if (tst_fs_type(filename) == TST_TMPFS_MAGIC) + goto out; + + fd = SAFE_OPEN(filename, O_RDONLY); + + fiemap = (struct fiemap *)SAFE_MALLOC(sizeof(struct fiemap) + sizeof(struct fiemap_extent)); + memset(fiemap, 0, sizeof(struct fiemap) + sizeof(struct fiemap_extent)); + + fiemap->fm_start = 0; + fiemap->fm_length = ~0; + fiemap->fm_flags = 0; + fiemap->fm_extent_count = 1; + + SAFE_IOCTL(fd, FS_IOC_FIEMAP, fiemap); + + /* + * fiemap->fm_mapped_extents != 1: + * This checks if the file does not have exactly one extent. If there are more + * or zero extents, the file is not stored in a single contiguous block. + * + * fiemap->fm_extents[0].fe_logical != 0: + * This checks if the first extent does not start at the logical offset 0 of + * the file. If it doesn't, it indicates that the file's first block of data + * is not at the beginning of the file, which implies non-contiguity. + * + * (fiemap->fm_extents[0].fe_flags & FIEMAP_EXTENT_LAST) != FIEMAP_EXTENT_LAST: + * This checks if the first extent does not have the FIEMAP_EXTENT_LAST flag set. + * If the flag isn't set, it means that this extent is not the last one, suggesting + * that there are more extents and the file is not contiguous. + */ + if (fiemap->fm_mapped_extents != 1 || + fiemap->fm_extents[0].fe_logical != 0 || + (fiemap->fm_extents[0].fe_flags & FIEMAP_EXTENT_LAST) != FIEMAP_EXTENT_LAST) { + + tst_res(TINFO, "File '%s' is not contiguous", filename); + contiguous = 0; + } + + SAFE_CLOSE(fd); + free(fiemap); + +out: + return contiguous; +} + /* * Make a swap file */ @@ -105,10 +162,15 @@ int make_swapfile(const char *swapfile, int safe) void is_swap_supported(const char *filename) { int i, sw_support = 0; - int fibmap = tst_fibmap(filename); + int ret = make_swapfile(filename, 1); + int fi_contiguous = file_is_contiguous(filename); long fs_type = tst_fs_type(filename); const char *fstype = tst_fs_type_name(fs_type); + if (fs_type == TST_BTRFS_MAGIC && + tst_kvercmp(5, 0, 0) < 0) + tst_brk(TCONF, "Swapfile on Btrfs (kernel < 5.0) not implemented"); + for (i = 0; swap_supported_fs[i]; i++) { if (strstr(fstype, swap_supported_fs[i])) { sw_support = 1; @@ -116,10 +178,8 @@ void is_swap_supported(const char *filename) } } - int ret = make_swapfile(filename, 1); - if (ret != 0) { - if (fibmap == 1 && sw_support == 0) + if (fi_contiguous == 0 && sw_support == 0) tst_brk(TCONF, "mkswap on %s not supported", fstype); else tst_brk(TFAIL, "mkswap on %s failed", fstype); @@ -129,7 +189,7 @@ void is_swap_supported(const char *filename) if (TST_RET == -1) { if (errno == EPERM) tst_brk(TCONF, "Permission denied for swapon()"); - else if (fibmap == 1 && errno == EINVAL && sw_support == 0) + else if (errno == EINVAL && fi_contiguous == 0 && sw_support == 0) tst_brk(TCONF, "Swapfile on %s not implemented", fstype); else tst_brk(TFAIL | TTERRNO, "swapon() on %s failed", fstype);