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=-9.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable 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 BC3E3C43467 for ; Fri, 16 Oct 2020 12:38:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6372A21582 for ; Fri, 16 Oct 2020 12:38:07 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=sargun.me header.i=@sargun.me header.b="fLa8iRJm" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2407683AbgJPMiF (ORCPT ); Fri, 16 Oct 2020 08:38:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404862AbgJPMiE (ORCPT ); Fri, 16 Oct 2020 08:38:04 -0400 Received: from mail-pg1-x541.google.com (mail-pg1-x541.google.com [IPv6:2607:f8b0:4864:20::541]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B5F9C0613DA for ; Fri, 16 Oct 2020 05:38:00 -0700 (PDT) Received: by mail-pg1-x541.google.com with SMTP id q21so1353804pgi.13 for ; Fri, 16 Oct 2020 05:38:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sargun.me; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=HqaYZUMkPSpLLEjOUbnHI46g98PupVda3GBY5xIdI7Y=; b=fLa8iRJm3pZRFs3LJk+qvkdZNLt8PKHk1DZ1czE6YVY+NyqomcV0WvQeqMm27MzkYu GwHr9Yx7aKxkI9KBqCEb0KK0gvKxVyLQRImBDRADJRXbhZCp0QZA8THonT0ckHS8r8Hr MD8XGgWjCGZjTRE12R7TPeev+20YhPQD/UcaE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=HqaYZUMkPSpLLEjOUbnHI46g98PupVda3GBY5xIdI7Y=; b=bmYPv/z3L4Yzrwrpp0Q55hpD2vc7vb8BD9khtZ3ZHIG49njirobY1S8UmMwtIooEj6 DWjvnKQSm8vOZ9dNnmTEZeySeGzJ6tF6ge4XSMsclCpYOMn2fSjFBmLoFTwR6mSpKmv5 Jfo6IDf/PsIE6r0I4QUR8NzwQzTIJyhrD8Uqy9/VhxHI6vIN1CKRshj3GziY/Zh/UdC7 kbb5vIG3DdI/nIZb0cImEZq44XSOWb2Y5dsQQJJB5EhQPw0IOtrkUaDaaSRtzGVuK9S5 Wpipd69PCRprtBysEQS942OxvIeXBcWhzrNICpOtpO99Y+36A/OXGAH2hgrJjXu9hzx6 mKXA== X-Gm-Message-State: AOAM531kFIfiuHFf/u4X7Us083asotMvXJPlFgY9vDCfQ1kii0q0LlXv EKtL8WL2xIN90AtaFAziz3WbMw== X-Google-Smtp-Source: ABdhPJxmrT2hmJIi2JVagSZ831T8sA2/y8kiYJuLUzDNhvARz+k4ExUL23VS3oEALSZOngdaE0FZLg== X-Received: by 2002:a63:4102:: with SMTP id o2mr2932789pga.354.1602851879506; Fri, 16 Oct 2020 05:37:59 -0700 (PDT) Received: from ubuntu.netflix.com (203.20.25.136.in-addr.arpa. [136.25.20.203]) by smtp.gmail.com with ESMTPSA id q8sm2857216pfg.118.2020.10.16.05.37.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Oct 2020 05:37:58 -0700 (PDT) From: Sargun Dhillon To: "J . Bruce Fields" , Chuck Lever , Trond Myklebust , Anna Schumaker , David Howells Cc: Sargun Dhillon , linux-nfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Al Viro , Kyle Anderson Subject: [PATCH v2 2/3] samples/vfs: Split out common code for new syscall APIs Date: Fri, 16 Oct 2020 05:37:44 -0700 Message-Id: <20201016123745.9510-3-sargun@sargun.me> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201016123745.9510-1-sargun@sargun.me> References: <20201016123745.9510-1-sargun@sargun.me> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There are a bunch of helper functions which make using the new mount APIs much easier. As we add examples of leveraging the new APIs, it probably makes sense to promote code reuse. Cc: David Howells Cc: Al Viro Cc: Kyle Anderson --- samples/vfs/Makefile | 2 + samples/vfs/test-fsmount.c | 86 +------------------------------------- samples/vfs/vfs-helper.c | 43 +++++++++++++++++++ samples/vfs/vfs-helper.h | 55 ++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 85 deletions(-) create mode 100644 samples/vfs/vfs-helper.c create mode 100644 samples/vfs/vfs-helper.h diff --git a/samples/vfs/Makefile b/samples/vfs/Makefile index 00b6824f9237..7f76875eaa70 100644 --- a/samples/vfs/Makefile +++ b/samples/vfs/Makefile @@ -1,5 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only +test-fsmount-objs := test-fsmount.o vfs-helper.o userprogs := test-fsmount test-statx + always-y := $(userprogs) userccflags += -I usr/include diff --git a/samples/vfs/test-fsmount.c b/samples/vfs/test-fsmount.c index 50f47b72e85f..36a4fa886200 100644 --- a/samples/vfs/test-fsmount.c +++ b/samples/vfs/test-fsmount.c @@ -14,91 +14,7 @@ #include #include #include - -#define E(x) do { if ((x) == -1) { perror(#x); exit(1); } } while(0) - -static void check_messages(int fd) -{ - char buf[4096]; - int err, n; - - err = errno; - - for (;;) { - n = read(fd, buf, sizeof(buf)); - if (n < 0) - break; - n -= 2; - - switch (buf[0]) { - case 'e': - fprintf(stderr, "Error: %*.*s\n", n, n, buf + 2); - break; - case 'w': - fprintf(stderr, "Warning: %*.*s\n", n, n, buf + 2); - break; - case 'i': - fprintf(stderr, "Info: %*.*s\n", n, n, buf + 2); - break; - } - } - - errno = err; -} - -static __attribute__((noreturn)) -void mount_error(int fd, const char *s) -{ - check_messages(fd); - fprintf(stderr, "%s: %m\n", s); - exit(1); -} - -/* Hope -1 isn't a syscall */ -#ifndef __NR_fsopen -#define __NR_fsopen -1 -#endif -#ifndef __NR_fsmount -#define __NR_fsmount -1 -#endif -#ifndef __NR_fsconfig -#define __NR_fsconfig -1 -#endif -#ifndef __NR_move_mount -#define __NR_move_mount -1 -#endif - - -static inline int fsopen(const char *fs_name, unsigned int flags) -{ - return syscall(__NR_fsopen, fs_name, flags); -} - -static inline int fsmount(int fsfd, unsigned int flags, unsigned int ms_flags) -{ - return syscall(__NR_fsmount, fsfd, flags, ms_flags); -} - -static inline int fsconfig(int fsfd, unsigned int cmd, - const char *key, const void *val, int aux) -{ - return syscall(__NR_fsconfig, fsfd, cmd, key, val, aux); -} - -static inline int move_mount(int from_dfd, const char *from_pathname, - int to_dfd, const char *to_pathname, - unsigned int flags) -{ - return syscall(__NR_move_mount, - from_dfd, from_pathname, - to_dfd, to_pathname, flags); -} - -#define E_fsconfig(fd, cmd, key, val, aux) \ - do { \ - if (fsconfig(fd, cmd, key, val, aux) == -1) \ - mount_error(fd, key ?: "create"); \ - } while (0) +#include "vfs-helper.h" int main(int argc, char *argv[]) { diff --git a/samples/vfs/vfs-helper.c b/samples/vfs/vfs-helper.c new file mode 100644 index 000000000000..bae2bc03c923 --- /dev/null +++ b/samples/vfs/vfs-helper.c @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#include +#include +#include +#include +#include "vfs-helper.h" + +void check_messages(int fd) +{ + char buf[4096]; + int err, n; + + err = errno; + + for (;;) { + n = read(fd, buf, sizeof(buf)); + if (n < 0) + break; + n -= 2; + + switch (buf[0]) { + case 'e': + fprintf(stderr, "Error: %*.*s\n", n, n, buf + 2); + break; + case 'w': + fprintf(stderr, "Warning: %*.*s\n", n, n, buf + 2); + break; + case 'i': + fprintf(stderr, "Info: %*.*s\n", n, n, buf + 2); + break; + } + } + + errno = err; +} + +__attribute__((noreturn)) +void mount_error(int fd, const char *s) +{ + check_messages(fd); + fprintf(stderr, "%s: %m\n", s); + exit(1); +} \ No newline at end of file diff --git a/samples/vfs/vfs-helper.h b/samples/vfs/vfs-helper.h new file mode 100644 index 000000000000..be460ab48247 --- /dev/null +++ b/samples/vfs/vfs-helper.h @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#include +#include +#include + +#define E(x) do { if ((x) == -1) { perror(#x); exit(1); } } while(0) + +/* Hope -1 isn't a syscall */ +#ifndef __NR_fsopen +#define __NR_fsopen -1 +#endif +#ifndef __NR_fsmount +#define __NR_fsmount -1 +#endif +#ifndef __NR_fsconfig +#define __NR_fsconfig -1 +#endif +#ifndef __NR_move_mount +#define __NR_move_mount -1 +#endif + +#define E_fsconfig(fd, cmd, key, val, aux) \ + do { \ + if (fsconfig(fd, cmd, key, val, aux) == -1) \ + mount_error(fd, key ?: "create"); \ + } while (0) + +static inline int fsopen(const char *fs_name, unsigned int flags) +{ + return syscall(__NR_fsopen, fs_name, flags); +} + +static inline int fsmount(int fsfd, unsigned int flags, unsigned int ms_flags) +{ + return syscall(__NR_fsmount, fsfd, flags, ms_flags); +} + +static inline int fsconfig(int fsfd, unsigned int cmd, + const char *key, const void *val, int aux) +{ + return syscall(__NR_fsconfig, fsfd, cmd, key, val, aux); +} + +static inline int move_mount(int from_dfd, const char *from_pathname, + int to_dfd, const char *to_pathname, + unsigned int flags) +{ + return syscall(__NR_move_mount, + from_dfd, from_pathname, + to_dfd, to_pathname, flags); +} + +__attribute__((noreturn)) +void mount_error(int fd, const char *s); +void check_messages(int fd); -- 2.25.1