Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp11225330rwd; Thu, 22 Jun 2023 10:15:18 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4lFhjWl+uMSTosOYcH7rgBrpiUuz7MNL3xEBqJYcmY8LQgZfJD7XLmM4N1RGrukttPW3if X-Received: by 2002:a17:902:ce8b:b0:1b6:a99b:cece with SMTP id f11-20020a170902ce8b00b001b6a99bcecemr4900305plg.30.1687454117964; Thu, 22 Jun 2023 10:15:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687454117; cv=none; d=google.com; s=arc-20160816; b=r+MkLlzn8bHrvgNssNbJpLDv6aD+mVq/2mUrgIqLIkC9QisTIP9ECTjr9eBJKp4jbc oj+GslMRLpTPGQdtYGM2gBCPwkUjeB1rlQocy89bYnL8fw5qKCTmwZTfEWJZHFQfS50O K9WIo1HXa4Dcj+5Vjh3FovVRwGE5Kymn926rCMLB+vgsb9ky9LNZoFVKgUh0XeVV4Au4 dvi8mQnCJC4FpHiVPTxpqFVxLjAgpZbjuVmb5i67tkGqFIMcclhezi2RJ5g3loiTQGxQ +eouhR5od6nWKMjY5VMQ8B65qKJic6uiZFsWFvED/cNeCcYS0Rl1P71lwjHXaw8OJ8U7 ZwVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=XIPYaudU5FYSMvmqvzeAFOUYfxsNw/JihAyfWPOa0Mk=; b=UiPFNd3ZYxPAxG8GZmtgIvUt/8lJNHTS/tuAmUcH1t8jo63tDGH6jVbcfL5brbhFIH aD2bUvcPY9IP3n9/87xomzNa1J5S8uPsEQQL1LNL1sSRz8ipg4cbZNktwX8rMVARmJoP mlTPY9vLKdbzS70o7y0YLpXaeZBh4UC/0YM1/HWBr30gbqMTab13AAd7A1AO4W8Hq+yU DqpI1LX6GiexE1+pSwFOh5rrltYRiZ8MaOph1leOL0N2TeBM3gtB7I6Twk2t8Ul/UbGe HA9lyy2574kGtVHhWk+FASLhD7Fa5ZHDSbc1SqOa3Di/7AgLFtnJCNva+KbzDoTmpx8f 4h2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@yandex.ru header.s=mail header.b=dtbxP3Vu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=yandex.ru Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c2-20020a170902d90200b001aafb271d13si6561558plz.235.2023.06.22.10.15.05; Thu, 22 Jun 2023 10:15:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@yandex.ru header.s=mail header.b=dtbxP3Vu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=yandex.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231500AbjFVQxk (ORCPT + 99 others); Thu, 22 Jun 2023 12:53:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231464AbjFVQxC (ORCPT ); Thu, 22 Jun 2023 12:53:02 -0400 Received: from forward103b.mail.yandex.net (forward103b.mail.yandex.net [178.154.239.150]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7448010A; Thu, 22 Jun 2023 09:53:00 -0700 (PDT) Received: from mail-nwsmtp-smtp-production-main-39.sas.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-39.sas.yp-c.yandex.net [IPv6:2a02:6b8:c08:2087:0:640:7bf5:0]) by forward103b.mail.yandex.net (Yandex) with ESMTP id EBB2F60042; Thu, 22 Jun 2023 19:52:55 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-39.sas.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id kqJAlpLDeKo0-WCEAxOU1; Thu, 22 Jun 2023 19:52:55 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1687452775; bh=XIPYaudU5FYSMvmqvzeAFOUYfxsNw/JihAyfWPOa0Mk=; h=Message-Id:Date:In-Reply-To:Cc:Subject:References:To:From; b=dtbxP3VuBYFTACo2/NNg8BgriPOGvD8mlH4JRSK8G1b7hIUI2xHHASVC+2UrivFQH RU+faJUezXDo45eBlcpYBRhWQObfeN0hHcuitQhLe1EGuwSWX/48SdlXPNlmTjnoZo AVQ0iOAdwGHtg1KM8ZXksCYIAD4O0SoMLLxxwP+E= Authentication-Results: mail-nwsmtp-smtp-production-main-39.sas.yp-c.yandex.net; dkim=pass header.i=@yandex.ru From: Stas Sergeev To: linux-kernel@vger.kernel.org Cc: Stas Sergeev , Jeff Layton , Chuck Lever , Alexander Viro , Christian Brauner , linux-fsdevel@vger.kernel.org, Shuah Khan , linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org Subject: [PATCH 2/2] selftests: add OFD lock tests Date: Thu, 22 Jun 2023 21:52:24 +0500 Message-Id: <20230622165225.2772076-3-stsp2@yandex.ru> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230622165225.2772076-1-stsp2@yandex.ru> References: <20230622165225.2772076-1-stsp2@yandex.ru> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Test the basic locking stuff on 2 fds: multiple read locks, conflicts between read and write locks, use of len==0 for queries. Also tests for F_UNLCK F_OFD_GETLK extension. Signed-off-by: Stas Sergeev CC: Jeff Layton CC: Chuck Lever CC: Alexander Viro CC: Christian Brauner CC: linux-fsdevel@vger.kernel.org CC: linux-kernel@vger.kernel.org CC: Shuah Khan CC: linux-kselftest@vger.kernel.org CC: linux-api@vger.kernel.org --- tools/testing/selftests/filelock/Makefile | 5 + tools/testing/selftests/filelock/ofdlocks.c | 132 ++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 tools/testing/selftests/filelock/Makefile create mode 100644 tools/testing/selftests/filelock/ofdlocks.c diff --git a/tools/testing/selftests/filelock/Makefile b/tools/testing/selftests/filelock/Makefile new file mode 100644 index 000000000000..478e82f8b464 --- /dev/null +++ b/tools/testing/selftests/filelock/Makefile @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 + +TEST_GEN_PROGS := ofdlocks + +include ../lib.mk diff --git a/tools/testing/selftests/filelock/ofdlocks.c b/tools/testing/selftests/filelock/ofdlocks.c new file mode 100644 index 000000000000..1ccb2b9b5ead --- /dev/null +++ b/tools/testing/selftests/filelock/ofdlocks.c @@ -0,0 +1,132 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include "../kselftest.h" + +static int lock_set(int fd, struct flock *fl) +{ + int ret; + + fl->l_pid = 0; // needed for OFD locks + fl->l_whence = SEEK_SET; + ret = fcntl(fd, F_OFD_SETLK, fl); + if (ret) + perror("fcntl()"); + return ret; +} + +static int lock_get(int fd, struct flock *fl) +{ + int ret; + + fl->l_pid = 0; // needed for OFD locks + fl->l_whence = SEEK_SET; + ret = fcntl(fd, F_OFD_GETLK, fl); + if (ret) + perror("fcntl()"); + return ret; +} + +int main(void) +{ + int rc; + struct flock fl, fl2; + int fd = open("/tmp/aa", O_RDWR | O_CREAT | O_EXCL, 0600); + int fd2 = open("/tmp/aa", O_RDONLY); + + unlink("aa"); + assert(fd != -1); + assert(fd2 != -1); + ksft_print_msg("[INFO] opened fds %i %i\n", fd, fd2); + + /* Set some read lock */ + fl.l_type = F_RDLCK; + fl.l_start = 5; + fl.l_len = 3; + rc = lock_set(fd, &fl); + if (rc == 0) { + ksft_print_msg + ("[SUCCESS] set OFD read lock on first fd\n"); + } else { + ksft_print_msg("[FAIL] to set OFD read lock on first fd\n"); + return -1; + } + /* Make sure read locks do not conflict on different fds. */ + fl.l_type = F_RDLCK; + fl.l_start = 5; + fl.l_len = 1; + rc = lock_get(fd2, &fl); + if (rc != 0) + return -1; + if (fl.l_type != F_UNLCK) { + ksft_print_msg("[FAIL] read locks conflicted\n"); + return -1; + } + /* Make sure read/write locks do conflict on different fds. */ + fl.l_type = F_WRLCK; + fl.l_start = 5; + fl.l_len = 1; + rc = lock_get(fd2, &fl); + if (rc != 0) + return -1; + if (fl.l_type != F_UNLCK) { + ksft_print_msg + ("[SUCCESS] read and write locks conflicted\n"); + } else { + ksft_print_msg + ("[SUCCESS] read and write locks not conflicted\n"); + return -1; + } + /* Get info about the lock on first fd. */ + fl.l_type = F_UNLCK; + fl.l_start = 5; + fl.l_len = 1; + rc = lock_get(fd, &fl); + if (rc != 0) { + ksft_print_msg + ("[FAIL] F_OFD_GETLK with F_UNLCK not supported\n"); + return -1; + } + if (fl.l_type != F_UNLCK) { + ksft_print_msg + ("[SUCCESS] F_UNLCK test returns: locked, type %i pid %i len %zi\n", + fl.l_type, fl.l_pid, fl.l_len); + } else { + ksft_print_msg + ("[FAIL] F_OFD_GETLK with F_UNLCK did not return lock info\n"); + return -1; + } + /* Try the same but by locking everything by len==0. */ + fl2.l_type = F_UNLCK; + fl2.l_start = 0; + fl2.l_len = 0; + rc = lock_get(fd, &fl2); + if (rc != 0) { + ksft_print_msg + ("[FAIL] F_OFD_GETLK with F_UNLCK not supported\n"); + return -1; + } + if (memcmp(&fl, &fl2, sizeof(fl))) { + ksft_print_msg + ("[FAIL] F_UNLCK test returns: locked, type %i pid %i len %zi\n", + fl.l_type, fl.l_pid, fl.l_len); + return -1; + } + ksft_print_msg("[SUCCESS] F_UNLCK with len==0 returned the same\n"); + /* Get info about the lock on second fd - no locks on it. */ + fl.l_type = F_UNLCK; + fl.l_start = 0; + fl.l_len = 0; + lock_get(fd2, &fl); + if (fl.l_type != F_UNLCK) { + ksft_print_msg + ("[FAIL] F_OFD_GETLK with F_UNLCK return lock info from another fd\n"); + return -1; + } + return 0; +} -- 2.39.2