From patchwork Wed Apr 19 16:29:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 9688007 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id DF83D6037F for ; Wed, 19 Apr 2017 16:29:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D0E4C283E2 for ; Wed, 19 Apr 2017 16:29:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C59FC28418; Wed, 19 Apr 2017 16:29:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0340A283E2 for ; Wed, 19 Apr 2017 16:29:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S937982AbdDSQ3Q (ORCPT ); Wed, 19 Apr 2017 12:29:16 -0400 Received: from mail-wr0-f196.google.com ([209.85.128.196]:34727 "EHLO mail-wr0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934068AbdDSQ3P (ORCPT ); Wed, 19 Apr 2017 12:29:15 -0400 Received: by mail-wr0-f196.google.com with SMTP id u18so3917610wrc.1; Wed, 19 Apr 2017 09:29:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=nxUWo6EkZo+hNTl/rQjWP1LXq53XnjbilLamRH1gFBU=; b=Lo/glfyF0Ltc8hilvfqVhc6x1yC/6YpGZM83cakNZ6XokFy94HQ5qpORQSgmm/Zm1m CmnsMWpp9zCPd2P77PNJbLZzucmeZb4yjCjX+EqaUsyvIgR8PV9RfWCF7y62zF2dLhzV UY57+tjPJZELfJuJ5ruZOV9YxzN2dYeNsQ3Ojuf1caPnGLJLJ9/CfJU8BFDH2irG6/d+ cnZeXZuHLRc3pfP2mGzNoR1v0hInl/AmTtWVlonra3FB2qZXU7W3EWvtnXkx8OqKAcih AHzahljK1WNY9QKUpwxrLKXsNj9bUFBx6qXCXJQjp5tzr1SeMU0515hquZBcPEH6K/EG CHdw== 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; bh=nxUWo6EkZo+hNTl/rQjWP1LXq53XnjbilLamRH1gFBU=; b=RUZk78+rZ9Y38lFXTr2SEJBqFkyn1zk4lWZMH+L9b1zf1Qiksu7oWJzzoafQB0MGdk chNM1TujLvpFUuDnu0g9lCUK1XyBdijh0vt0Zoc9PzgH0d6qgEFEFRl6aVpfDhcKJrmc OnwljZyw4sSd9jXZO1suZ5AvtMe6inQUH3EqSlWmmp9P7uYTMeT7SbwhROIpmmLeZQ6H 6hrUJ+m7WyN8oYC1m9nhzb3P3dEIjRZsKyEPibNuv9EMvSf504Vbm+1twmFhY4rbHobS T4rAXPVUwa4lMCFsXAZEjAoFZKElCqUMi+guJuQodfFzsyTdyxRzO3K/GmSO8G3XmT+g duTQ== X-Gm-Message-State: AN3rC/4w/BKEki32DHLbeogMhn65c2iVCBBkDCT8BCIJixF1MjhViibv bGnMJtYlzsJJuA== X-Received: by 10.223.142.143 with SMTP id q15mr3625198wrb.180.1492619353797; Wed, 19 Apr 2017 09:29:13 -0700 (PDT) Received: from amir-VirtualBox.ctera.local (bzq-166-168-31-246.red.bezeqint.net. [31.168.166.246]) by smtp.gmail.com with ESMTPSA id l19sm3983981wre.2.2017.04.19.09.29.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 19 Apr 2017 09:29:13 -0700 (PDT) From: Amir Goldstein To: Eryu Guan Cc: Trond Myklebust , Jeff Layton , "J . Bruce Fields" , David Howells , Miklos Szeredi , fstests@vger.kernel.org, linux-unionfs@vger.kernel.org Subject: [PATCH v2 3/4] src/open_by_handle: flexible usage options Date: Wed, 19 Apr 2017 19:29:18 +0300 Message-Id: <1492619359-24763-4-git-send-email-amir73il@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1492619359-24763-1-git-send-email-amir73il@gmail.com> References: <1492619359-24763-1-git-send-email-amir73il@gmail.com> Sender: fstests-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: fstests@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP More usage options for testing open_by_handle, which are needed for testing stable handles across copy up in overlayfs. usage: open_by_handle [-c|-l|-u|-d] [num_files] Examples: 1. Create test set of N files and try to get their NFS handles: open_by_handle -c [N] This is used by new helper _require_exportfs() to check if filesystem supports exportfs 2. Get file handles for existing test set, drop caches and try to open all files by handle: open_by_handle [N] 3. Get file handles for existing test set, unlink all test files, drop caches, try to open all files by handle and expect ESTALE: open_by_handle -d [N] 4. Get file handles for existing test set, hardlink all test files, then unlink the original files, drop caches and try to open all files by handle (should work): open_by_handle -l [N] open_by_handle -u [N] This test is done with 2 invocations of the program, first to hardlink (-l) and then to unlink the originals (-u), because we would like to be able to perform the hardlinks on overlay lower layer and unlink on upper layer. NOTE that open_by_handle -u doesn't check if the files are hardlinked, it just assumes that they are. If they are not then the test will fail, because file handles would be stale. Signed-off-by: Amir Goldstein --- common/rc | 10 +++ doc/auxiliary-programs.txt | 16 ++++- doc/requirement-checking.txt | 7 ++ src/open_by_handle.c | 156 +++++++++++++++++++++++++++++++++++++------ 4 files changed, 167 insertions(+), 22 deletions(-) diff --git a/common/rc b/common/rc index 685b859..a706920 100644 --- a/common/rc +++ b/common/rc @@ -2857,6 +2857,16 @@ _require_freeze() [ $result -eq 0 ] || _notrun "$FSTYP does not support freezing" } +# Does NFS export work on this fs? +_require_exportfs() +{ + _require_test_program "open_by_handle" + mkdir -p "$TEST_DIR"/exportfs_test + $here/src/open_by_handle -c "$TEST_DIR"/exportfs_test 2>&1 \ + || _notrun "$FSTYP does not support NFS export" +} + + # Does shutdown work on this fs? _require_scratch_shutdown() { diff --git a/doc/auxiliary-programs.txt b/doc/auxiliary-programs.txt index 17797b0..2e2060a 100644 --- a/doc/auxiliary-programs.txt +++ b/doc/auxiliary-programs.txt @@ -15,9 +15,10 @@ note the dependency with: Contents: - - af_unix -- Create an AF_UNIX socket - - stat_test -- statx syscall exercise - - xfs_io -- General I/O operation exercise + - af_unix -- Create an AF_UNIX socket + - open_by_handle -- open_by_handle_at syscall exercise + - stat_test -- statx syscall exercise + - xfs_io -- General I/O operation exercise ================== @@ -28,6 +29,15 @@ af_unix The af_unix program creates an AF_UNIX socket at the given location. +open_by_handle + + The open_by_handle program exercises the open_by_handle_at() system + call. It can check if file handles are valid or stale after certain + filesystem operations. + + See also: + _require_exportfs + stat_test The stat_test program is primarily designed to exercise the statx() diff --git a/doc/requirement-checking.txt b/doc/requirement-checking.txt index 730c7ac..95d10e6 100644 --- a/doc/requirement-checking.txt +++ b/doc/requirement-checking.txt @@ -15,6 +15,7 @@ they have. This is done with _require_ macros, which may take parameters. (2) Filesystem capability requirements. _require_chattr + _require_exportfs (3) System call requirements. @@ -86,6 +87,12 @@ _require_chattr tests to see if setting the append-only and immutable attributes on a file (chattr +a +i) is supported. +_require_exportfs + + The test requires that the $TEST_DEV filesystem supports NFS export. + The test also requires the use of the open_by_handle_at() system call and + will be skipped if it isn't available in the kernel. + ======================== SYSTEM CALL REQUIREMENTS diff --git a/src/open_by_handle.c b/src/open_by_handle.c index 76510ff..63ebdac 100644 --- a/src/open_by_handle.c +++ b/src/open_by_handle.c @@ -25,6 +25,46 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/* + +usage: open_by_handle [-c|-l|-u|-d] [num_files] + +Examples: + +1. Create test set of N files and try to get their NFS handles: + + open_by_handle -c [N] + + This is used by new helper _require_exportfs() to check + if filesystem supports exportfs + +2. Get file handles for existing test set, drop caches and try to + open all files by handle: + + open_by_handle [N] + +3. Get file handles for existing test set, unlink all test files, + drop caches, try to open all files by handle and expect ESTALE: + + open_by_handle -d [N] + +4. Get file handles for existing test set, hardlink all test files, + then unlink the original files, drop caches and try to open all + files by handle (should work): + + open_by_handle -l [N] + open_by_handle -u [N] + + This test is done with 2 invocations of the program, first to + hardlink (-l) and then to unlink the originals (-u), because + we would like to be able to perform the hardlinks on overlay + lower layer and unlink on upper layer. + + NOTE that open_by_handle -u doesn't check if the files are + hardlinked, it just assumes that they are. If they are not + then the test will fail, because file handles would be stale. +*/ + #include #include #include @@ -35,41 +75,85 @@ #include #include -#define NUMFILES 1024 +#define MAXFILES 1024 struct handle { struct file_handle fh; unsigned char fid[MAX_HANDLE_SZ]; -} handle[NUMFILES]; +} handle[MAXFILES]; + +void usage(void) +{ + fprintf(stderr, "usage: open_by_handle [-c|-l|-u|-d] [num_files]\n"); + fprintf(stderr, "\n"); + fprintf(stderr, "open_by_handle -c [N] - create N test files under test_dir, try to get file handles and exit\n"); + fprintf(stderr, "open_by_handle [N] - get file handles of test files, drop caches and try to open by handle\n"); + fprintf(stderr, "open_by_handle -l [N] - create hardlinks to test files, drop caches and try to open by handle\n"); + fprintf(stderr, "open_by_handle -u [N] - unlink (hardlinked) test files, drop caches and try to open by handle\n"); + fprintf(stderr, "open_by_handle -d [N] - unlink test files and hardlinks, drop caches and try to open by handle\n"); + exit(EXIT_FAILURE); +} int main(int argc, char **argv) { - int i; + int i, c; int fd; int ret; int failed = 0; char fname[PATH_MAX]; + char fname2[PATH_MAX]; char *test_dir; int mount_fd, mount_id; + int numfiles = 1; + int create = 0, delete = 0, nlink = 1; - if (argc != 2) { - fprintf(stderr, "usage: open_by_handle \n"); - return EXIT_FAILURE; + if (argc < 2 || argc > 4) + usage(); + + while ((c = getopt(argc, argv, "clud")) != -1) { + switch (c) { + case 'c': + create = 1; + break; + case 'l': + nlink = 2; + break; + case 'u': + delete = 1; + nlink = 1; + break; + case 'd': + delete = 1; + nlink = 0; + break; + default: + fprintf(stderr, "illegal option '%s'\n", argv[optind]); + case 'h': + usage(); + } + } + if (optind == argc || optind > 2) + usage(); + test_dir = argv[optind++]; + if (optind < argc) + numfiles = atoi(argv[optind]); + if (!numfiles || numfiles > MAXFILES) { + fprintf(stderr, "illegal value '%s' for num_files\n", argv[optind]); + usage(); } - test_dir = argv[1]; mount_fd = open(test_dir, O_RDONLY|O_DIRECTORY); if (mount_fd < 0) { - perror("open test_dir"); + perror(test_dir); return EXIT_FAILURE; } /* - * create a large number of files to force allocation of new inode - * chunks on disk. + * Create the test files and remove leftover hardlinks from previous run */ - for (i=0; i < NUMFILES; i++) { + for (i=0; create && i < numfiles; i++) { sprintf(fname, "%s/file%06d", test_dir, i); + sprintf(fname2, "%s/link%06d", test_dir, i); fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0644); if (fd < 0) { printf("Warning (%s,%d), open(%s) failed.\n", __FILE__, __LINE__, fname); @@ -77,13 +161,19 @@ int main(int argc, char **argv) return EXIT_FAILURE; } close(fd); + /* blow up leftovers hardlinks if they exist */ + ret = unlink(fname2); + if (ret < 0 && errno != ENOENT) { + perror("unlink"); + return EXIT_FAILURE; + } } /* sync to get the new inodes to hit the disk */ sync(); /* create the handles */ - for (i=0; i < NUMFILES; i++) { + for (i=0; i < numfiles; i++) { sprintf(fname, "%s/file%06d", test_dir, i); handle[i].fh.handle_bytes = MAX_HANDLE_SZ; ret = name_to_handle_at(AT_FDCWD, fname, &handle[i].fh, &mount_id, 0); @@ -93,14 +183,37 @@ int main(int argc, char **argv) } } + /* after creating test set only check that fs supports exportfs */ + if (create) + return EXIT_SUCCESS; + + /* hardlink the files */ + for (i=0; nlink > 1 && i < numfiles; i++) { + sprintf(fname, "%s/file%06d", test_dir, i); + sprintf(fname2, "%s/link%06d", test_dir, i); + ret = link(fname, fname2); + if (ret < 0) { + perror("link"); + return EXIT_FAILURE; + } + } + /* unlink the files */ - for (i=0; i < NUMFILES; i++) { + for (i=0; delete && i < numfiles; i++) { sprintf(fname, "%s/file%06d", test_dir, i); + sprintf(fname2, "%s/link%06d", test_dir, i); ret = unlink(fname); if (ret < 0) { perror("unlink"); return EXIT_FAILURE; } + /* with -d flag, delete the hardlink if it exists */ + if (!nlink) + ret = unlink(fname2); + if (ret < 0 && errno != ENOENT) { + perror("unlink"); + return EXIT_FAILURE; + } } /* sync to get log forced for unlink transactions to hit the disk */ @@ -121,20 +234,25 @@ int main(int argc, char **argv) } /* - * now try to open the files by the stored handles. Expecting ENOENT - * for all of them. + * now try to open the files by the stored handles. Expecting ESTALE + * if all files and their hardlinks have been unlinked. */ - for (i=0; i < NUMFILES; i++) { + for (i=0; i < numfiles; i++) { errno = 0; fd = open_by_handle_at(mount_fd, &handle[i].fh, O_RDWR); - if (fd < 0 && (errno == ENOENT || errno == ESTALE)) { + if (nlink && fd >= 0) { + close(fd); + continue; + } else if (!nlink && fd < 0 && (errno == ENOENT || errno == ESTALE)) { continue; } if (fd >= 0) { printf("open_by_handle(%d) opened an unlinked file!\n", i); close(fd); - } else - printf("open_by_handle(%d) returned %d incorrectly on an unlinked file!\n", i, errno); + } else { + printf("open_by_handle(%d) returned %d incorrectly on %s file!\n", i, errno, + nlink ? "a linked" : "an unlinked"); + } failed++; } if (failed)